在之前的版本是可以使用UDID獲取iOS設(shè)備唯一標(biāo)識,
NSString *udidString = [[UIDevice currentDevice] uniqueIdentifier];
但是iOS5及以后蕉鸳,被蘋果禁止使用了(棄用了)
而直接獲取的UUID系統(tǒng)不會存儲乎赴,每次調(diào)用的時候都會獲得一個新的UUID標(biāo)示符
一般獲取UUID的方法如下
// 普通的獲取UUID的方法
+ (NSString *)getUUID {
CFUUIDRef puuid = CFUUIDCreate( nil );
CFStringRef uuidString = CFUUIDCreateString(nil, puuid);
NSString *result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));
return result;
}
我們可以通過持久存儲這個標(biāo)識符,來保證即使重新加載潮尝,刪除后重裝應(yīng)用都能夠唯一標(biāo)識榕吼,以下的方式通過獲取到UUID后存入系統(tǒng)中的keychain中,來保證以后每次可以得到相同的唯一標(biāo)志勉失。
代碼如下:
#import "UUIDTool.h"
// kUUIDKey盡量改成便于區(qū)分App的(有簡友說如果一個手機上兩個App都這樣用相同的kUUIDKey可能會存在bug羹蚣,我測試幾次沒發(fā)現(xiàn),但是還是建議盡量要區(qū)分開)
NSString * const kUUIDKey = @"com.myApp.uuid";
@implementation UUIDTool
#pragma mark - 獲取到UUID后存入系統(tǒng)中的keychain中
+ (NSString *)getUUIDInKeychain {
// 1.直接從keychain中獲取UUID
NSString *getUDIDInKeychain = (NSString *)[UUIDTool load:kUUIDKey];
NSLog(@"從keychain中獲取UUID%@", getUDIDInKeychain);
// 2.如果獲取不到乱凿,需要生成UUID并存入系統(tǒng)中的keychain
if (!getUDIDInKeychain || [getUDIDInKeychain isEqualToString:@""] || [getUDIDInKeychain isKindOfClass:[NSNull class]]) {
// 2.1 生成UUID
CFUUIDRef puuid = CFUUIDCreate(nil);
CFStringRef uuidString = CFUUIDCreateString(nil, puuid);
NSString *result = (NSString *)CFBridgingRelease(CFStringCreateCopy(NULL, uuidString));
CFRelease(puuid);
CFRelease(uuidString);
NSLog(@"生成UUID:%@",result);
// 2.2 將生成的UUID保存到keychain中
[UUIDTool save:kUUIDKey data:result];
// 2.3 從keychain中獲取UUID
getUDIDInKeychain = (NSString *)[UUIDTool load:kUUIDKey];
}
return getUDIDInKeychain;
}
#pragma mark - 刪除存儲在keychain中的UUID
+ (void)deleteKeyChain {
[self delete:kUUIDKey];
}
#pragma mark - 私有方法
+ (NSMutableDictionary *)getKeyChainQuery:(NSString *)service {
return [NSMutableDictionary dictionaryWithObjectsAndKeys:(id)kSecClassGenericPassword,(id)kSecClass,service,(id)kSecAttrService,service,(id)kSecAttrAccount,(id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible, nil];
}
// 從keychain中獲取UUID
+ (id)load:(NSString *)service {
id ret = nil;
NSMutableDictionary *keychainQuery = [self getKeyChainQuery:service];
[keychainQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
[keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
CFDataRef keyData = NULL;
if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
@try {
ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
}
@catch (NSException *exception) {
NSLog(@"Unarchive of %@ failed: %@", service, exception);
}
@finally {
NSLog(@"finally");
}
}
if (keyData) {
CFRelease(keyData);
}
NSLog(@"ret = %@", ret);
return ret;
}
+ (void)delete:(NSString *)service {
NSMutableDictionary *keychainQuery = [self getKeyChainQuery:service];
SecItemDelete((CFDictionaryRef)keychainQuery);
}
// 將生成的UUID保存到keychain中
+ (void)save:(NSString *)service data:(id)data {
// Get search dictionary
NSMutableDictionary *keychainQuery = [self getKeyChainQuery:service];
// Delete old item before add new item
SecItemDelete((CFDictionaryRef)keychainQuery);
// Add new object to search dictionary(Attention:the data format)
[keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];
// Add item to keychain with the search dictionary
SecItemAdd((CFDictionaryRef)keychainQuery, NULL);
}
另外附上GitHub上的Demo鏈接:https://github.com/rambocai/ZYUUID-Emon.git