MyUUIDManager.h
#import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interface MyUUIDManager : NSObject +(void)saveUUID:(NSString *)uuid; +(NSString *)getUUID; +(void)deleteUUID; @end NS_ASSUME_NONNULL_END
MyUUIDManager.m
#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
MyKeyChainManager.h
#import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interface MyKeyChainManager : NSObject + (void)save:(NSString *)service data:(id)data; + (id)load:(NSString *)service ; + (void)delete:(NSString *)service ; @end NS_ASSUME_NONNULL_END
MyKeyChainManager.m
#import "MyKeyChainManager.h" @implementation MyKeyChainManager + (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) { } @finally { } } return ret; } + (void)delete:(NSString *)service { NSMutableDictionary *keychainQuery = [self getKeychainQuery:service]; SecItemDelete((__bridge_retained CFDictionaryRef)keychainQuery); } @end
使用:NSLog(@"%@",[MyUUIDManager getUUID]);
//如果uuid为空
if(!uuid){ // 获取UUID 这个是要引入<AdSupport/AdSupport.h>的 if ( [[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled] ) { uuid = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]; return uuid; }else{ return @""; } }