Ios 獲取手機不變的UDID

感謝大神的解惑? ? ? ??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);

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容