很多的應用都需要用到手機的唯一標示,而且要求這個唯一標示不能因為應用app的卸載或者改變而變化。
在iOS7以前是可以通過Mac地址來實現這個功能的,但是iOS7(包含)以後是無法獲得Mac地址的;蘋果官方推薦使用UUID,但是每次隨著APP的卸載重裝,UUID會隨之發生變化,那該如何處理呢?
我們需要一個能在app卸載重裝後不會改變的值,而keyChain恰巧就可以做到。配合UUID就可以實現了!讓我們來分析下:
1.我們首先需要導入Security.frameWork(keychain依賴它),然後需要一個keychain管理器,一個uuid管理器,文件組成如下:
2.首先來看MyKeychainManager,其實就是對keychain的增、刪、改、查,類似於數據庫的處理。
先通過.h文件來開放下增、刪、改、查四個接口:
#import@interface MyKeyChainManager : NSObject + (NSMutableDictionary *)getKeychainQuery:(NSString *)service; + (void)save:(NSString *)service data:(id)data; + (id)load:(NSString *)service; + (void)delete:(NSString *)service; @end
.m文件實現接口,keychain的使用網上很多,直接貼代碼了:
#import MyKeyChainManager.h @implementation MyKeyChainManager : NSObject + (NSMutableDictionary *)getKeychainQuery:(NSString *)service { return [NSMutableDictionary dictionaryWithObjectsAndKeys: (__bridge_transfer id)kSecClassGenericPassword,(__bridge_transfer id)kSecClass, service, (__bridge_transfer id)kSecAttrService, service, (__bridge_transfer id)kSecAttrAccount, (__bridge_transfer id)kSecAttrAccessibleAfterFirstUnlock,(__bridge_transfer id)kSecAttrAccessible, nil]; } + (void)save:(NSString *)service data:(id)data { //Get search dictionary NSMutableDictionary *keychainQuery = [self getKeychainQuery:service]; //Delete old item before add new item SecItemDelete((__bridge_retained CFDictionaryRef)keychainQuery); //Add new object to search dictionary(Attention:the data format) [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge_transfer id)kSecValueData]; //Add item to keychain with the search dictionary SecItemAdd((__bridge_retained CFDictionaryRef)keychainQuery, NULL); } + (id)load:(NSString *)service { id ret = nil; NSMutableDictionary *keychainQuery = [self getKeychainQuery:service]; //Configure the search setting [keychainQuery setObject:(id)kCFBooleanTrue forKey:(__bridge_transfer id)kSecReturnData]; [keychainQuery setObject:(__bridge_transfer id)kSecMatchLimitOne forKey:(__bridge_transfer id)kSecMatchLimit]; CFDataRef keyData = NULL; if (SecItemCopyMatching((__bridge_retained CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) { @try { ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge_transfer NSData *)keyData]; } @catch (NSException *e) { NSLog(@Unarchive of %@ failed: %@, service, e); } @finally { } } return ret; } + (void)delete:(NSString *)service { NSMutableDictionary *keychainQuery = [self getKeychainQuery:service]; SecItemDelete((__bridge_retained CFDictionaryRef)keychainQuery); } @end
#import@interface MyUUIDManager: NSObject +(void)saveUUID:(NSString *)uuid; +(NSString *)getUUID; +(void)deleteUUID; @end
#import MyUUIDManager.h #import MyKeyChainManager.h @implementation MyUUIDManager static NSString * const KEY_IN_KEYCHAIN = @com.myuuid.uuid; +(void)saveUUID:(NSString *)uuid{ if (uuid && uuid.length > 0) { [MyKeyChainManager save:KEY_IN_KEYCHAIN data:uuid]; } } +(NSString *)getUUID{ //先獲取keychain裡面的UUID字段,看是否存在 NSString *uuid = (NSString *)[MyKeyChainManager load:KEY_IN_KEYCHAIN]; //如果不存在則為首次獲取UUID,所以獲取保存。 if (!uuid || uuid.length == 0) { CFUUIDRef puuid = CFUUIDCreate( nil ); CFStringRef uuidString = CFUUIDCreateString( nil, puuid ); uuid = [NSString stringWithFormat:@%@, uuidString]; [self saveUUID:uuid]; CFRelease(puuid); CFRelease(uuidString); } return uuid; } +(void)deleteUUID{ [MyKeyChainManager delete:KEY_IN_KEYCHAIN]; } @end
NSString *uuid = [MyUUIDManager getUUID]; NSLog(@uuid: %@,uuid);
2015-08-10 18:14:07.641 MyTest[3190:220331] uuid: 839E055B-09A5-42E1-A46C-DF4481E23333
2015-08-10 18:22:37.122 MyTest[3214:222053] uuid: 839E055B-09A5-42E1-A46C-DF4481E23333