前言:IOS7中,UDID和MAC地址等设备唯一标识方法都被淘汰,但开发中,业务往往需要这个唯一标识,经过研究,还没找到可以完全替代UDID和MAC地址的方法,但退而求其次,找到了将UUID作为UDID使用的办法,UUID保证了序列号的唯一性,所以剩下问题就是解决如何保存UUID的问题了,经过调研,利用KeyChain可以保存数据,并且APP删除,重启机器情况下仍然不影响保存的数据,但如果用户刷系统,这种办法就不行了。下面奉上代码:
项目要引入:Security.framework
#import <UIKit/UIKit.h> #import "KeychainHelper.h" @interface ViewController : UIViewController @end
#import "ViewController.h" NSString * const KEY_DIC = @"com.company.app.dic"; NSString * const KEY_UDID = @"com.company.app.kUdidTest"; @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (NSString*) uuid { CFUUIDRef puuid = CFUUIDCreate(nil); CFStringRef uuidString = CFUUIDCreateString(nil, puuid); NSString *result = (NSString *)CFStringCreateCopy(NULL, uuidString); CFRelease(puuid); CFRelease(uuidString); return [result autorelease]; } - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSMutableDictionary *dic = (NSMutableDictionary *)[KeychainHelper load:KEY_DIC]; NSString *udid = [dic objectForKey:KEY_UDID]; if (udid) { NSLog(@"udid:%@", udid); } if (!udid) { NSLog(@"save"); NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [dic setObject:[self uuid] forKey:KEY_UDID]; [KeychainHelper save:KEY_DIC data:dic]; } } @end
-------------------分割线---------------------
#import <Foundation/Foundation.h> #import <Security/Security.h> @interface KeychainHelper : NSObject + (void) save:(NSString *)service data:(id)data; + (id) load:(NSString *)service; + (void) deleteData:(NSString *)service; @end
#import "KeychainHelper.h" @implementation KeychainHelper + (NSMutableDictionary *)getKeychainQuery:(NSString *)service { return [NSMutableDictionary dictionaryWithObjectsAndKeys: (id)kSecClassGenericPassword,(id)kSecClass, service, (id)kSecAttrService, service, (id)kSecAttrAccount, (id)kSecAttrAccessibleAfterFirstUnlock,(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((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); } + (id)load:(NSString *)service { id ret = nil; NSMutableDictionary *keychainQuery = [self getKeychainQuery:service]; //Configure the search setting //Since in our simple case we are expecting only a single attribute to be returned (the password) we can set the attribute kSecReturnData to kCFBooleanTrue [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:(NSData *)keyData]; } @catch (NSException *e) { NSLog(@"Unarchive of %@ failed: %@", service, e); } @finally { } } if (keyData) CFRelease(keyData); return ret; } + (void)deleteData:(NSString *)service { NSMutableDictionary *keychainQuery = [self getKeychainQuery:service]; SecItemDelete((CFDictionaryRef)keychainQuery); } @end