.h
+(NSString *)getUUID;
.m
//当前时间(毫秒级)+随机数和打乱的字符串 进行洗牌(洗牌算法)
+(NSString *)getUUID{
NSString* date;
NSDateFormatter * formatter = [[NSDateFormatter alloc ] init];
[formatter setDateFormat:@"YYYYMMddhhmmssSSS"];
date = [formatter stringFromDate:[NSDate date]];
NSString * timeNow = [[NSString alloc] initWithFormat:@"%@", date];
int x = (arc4random() % 1001) + 8999;
NSString *alphabet = @"ABCDEFGHIJK";
// Get the characters into a C array for efficient shuffling
NSUInteger numberOfCharacters = [alphabet length];
unichar *characters = calloc(numberOfCharacters, sizeof(unichar));
[alphabet getCharacters:characters range:NSMakeRange(0, numberOfCharacters)];
// Perform a Fisher-Yates shuffle
for (NSUInteger i = 0; i < numberOfCharacters; ++i) {
NSUInteger j = (arc4random_uniform(numberOfCharacters - i) + i);
unichar c = characters[i];
characters[i] = characters[j];
characters[j] = c;
}
NSString *result = [NSString stringWithCharacters:characters length:numberOfCharacters];
free(characters);
NSString *UDID = [NSString stringWithFormat:@"%@%d%@",timeNow,x,result];
NSString *alphabet1 = UDID;
// Get the characters into a C array for efficient shuffling
NSUInteger numberOfCharacters1 = [alphabet1 length];
unichar *characters1 = calloc(numberOfCharacters1, sizeof(unichar));
[alphabet1 getCharacters:characters1 range:NSMakeRange(0, numberOfCharacters1)];
// Perform a Fisher-Yates shuffle
for (NSUInteger i = 0; i < numberOfCharacters1; ++i) {
NSUInteger j = (arc4random_uniform(numberOfCharacters1 - i) + i);
unichar c = characters1[i];
characters1[i] = characters1[j];
characters1[j] = c;
}
NSString *UUID = [NSString stringWithCharacters:characters1 length:numberOfCharacters1];
free(characters1);
NSMutableString *uuid2 = [[NSMutableString alloc] initWithString:UUID];
[uuid2 insertString:@"-" atIndex:8];
[uuid2 insertString:@"-" atIndex:13];
[uuid2 insertString:@"-" atIndex:18];
[uuid2 insertString:@"-" atIndex:23];
return uuid2;
}
//这个想法不是很严谨,不能完全的保证唯一性。
//在数据量不是特别大或者特定情况下可以当做唯一标示符使用。