感謝大神的解惑? ? ? ??https://www.cnblogs.com/smileEvday/p/UDID.html
導入頭文件
#import? <AdSupport/AdSupport.h>
#import "KeyChainStore.h"
廢話不多說,直接上代碼,
+(NSString *)CkeckUUID {
? ? // 這個key的前綴最好是你的BundleID
? ? NSString*strUUID = (NSString*)[KeyChainStoreload:@"com.skrApp"];
? ? //首次執(zhí)行該方法時,uuid為空
? ? if([strUUIDisEqualToString:@""]|| !strUUID)
? ? {
? ? ? ? // 獲取UUID 這個是要引入的
? ? ? ? strUUID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
? ? ? ? if(strUUID.length ==0 || [strUUID isEqualToString:@"00000000-0000-0000-0000-000000000000"])
? ? ? ? {
? ? ? ? ? ? //生成一個uuid的方法
? ? ? ? ? ? CFUUIDRef uuidRef= CFUUIDCreate(kCFAllocatorDefault);
? ? ? ? ? ? strUUID = (NSString*)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault,uuidRef));
? ? ? ? ? ? CFRelease(uuidRef);
? ? ? ? }
? ? ? ? //將該uuid保存到keychain
? ? ? ? [KeyChainStoresave:@"com.skrApp"data:strUUID];
? ? }
? ? returnstrUUID;
}
KeyChainStore.h 文件
#import
NS_ASSUME_NONNULL_BEGIN
@interface KeyChainStore : NSObject
+ (void)save:(NSString*)servicedata:(id)data;
+ (id)load:(NSString*)service;
+ (void)deleteKeyData:(NSString*)service;
@end
NS_ASSUME_NONNULL_END
KeyChainStore.m 文件
#import "KeyChainStore.h"
@implementation KeyChainStore
+ (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*)servicedata:(id)data{
? ? //Get search dictionary
? ? NSMutableDictionary*keychainQuery = [selfgetKeychainQuery:service];
? ? //Delete old item before add new item
? ? SecItemDelete((CFDictionaryRef)keychainQuery);
? ? //Add new object to searchdictionary(Attention:the data format)
? ? [keychainQuerysetObject:[NSKeyedArchiver archivedDataWithRootObject:data]forKey:(id)kSecValueData];
? ? //Add item to keychain with the searchdictionary
? ? SecItemAdd((CFDictionaryRef)keychainQuery,NULL);
}
+ (id)load:(NSString*)service {
? ? idret =nil;
? ? NSMutableDictionary*keychainQuery = [selfgetKeychainQuery:service];
? ? //Configure the search setting
? ? //Since in our simple case we areexpecting only a single attribute to be returned (the password) wecan set the attribute kSecReturnData to kCFBooleanTrue
? ? [keychainQuerysetObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
? ? [keychainQuerysetObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
? ? CFDataRefkeyData =NULL;
? ? if(SecItemCopyMatching((CFDictionaryRef)keychainQuery,(CFTypeRef*)&keyData) ==noErr){
? ? ? ? @try{
? ? ? ? ? ? ret =[NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData*)keyData];
? ? ? ? }@catch(NSException *e) {
? ? ? ? ? ? NSLog(@"Unarchiveof %@ failed: %@",service, e);
? ? ? ? }@finally{
? ? ? ? }
? ? }
? ? if(keyData)
? ? ? ? CFRelease(keyData);
? ? returnret;
}
+ (void)deleteKeyData:(NSString*)service {
? ? NSMutableDictionary*keychainQuery = [selfgetKeychainQuery:service];
? ? SecItemDelete((CFDictionaryRef)keychainQuery);
}