擷取iOS裝置UUID

來源:互聯網
上載者:User

標籤:account   div   emc   man   attr   eric   manage   ecc   test   

擷取到UUID,然後把UUID儲存到KeyChain裡面。

這樣以後即使卸載APP,也可以從KeyChain中讀取回來。

但是刷機或重裝系統後uuid還是會改變。

代碼採用CFUUID+KeyChain的實現方式:

1.建立KeyChain管理類

@implementation ZZKeyChainManager+ (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)deleteUUID:(NSString *)service {    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];    SecItemDelete((__bridge_retained CFDictionaryRef)keychainQuery);}@end

2.建立UUID管理類

#import "ZZUUIDManager.h"@implementation ZZUUIDManagerstatic NSString * const KEY_IN_KEYCHAIN = @"com.zzuuid.uuid";+(void)saveUUID:(NSString *)uuid{    if (uuid && uuid.length > 0) {        [ZZKeyChainManager save:KEY_IN_KEYCHAIN data:uuid];    }}+(NSString *)getUUID{    NSString *getUDIDInKeychain = (NSString *)[ZZKeyChainManager load:KEY_IN_KEYCHAIN];    if (!getUDIDInKeychain || [getUDIDInKeychain isEqualToString:@""] || [getUDIDInKeychain isKindOfClass:[NSNull class]]) {        CFUUIDRef puuid = CFUUIDCreate(nil);        CFStringRef uuidString = CFUUIDCreateString(nil, puuid );        NSString * result = (NSString *)CFBridgingRelease(CFStringCreateCopy(NULL, uuidString));        CFRelease(puuid);        CFRelease(uuidString);        getUDIDInKeychain = result;               [ZZKeyChainManager save:KEY_IN_KEYCHAIN data:result];        getUDIDInKeychain = (NSString *)[ZZKeyChainManager load:KEY_IN_KEYCHAIN];    }    return getUDIDInKeychain;}+(void)deleteUUID{    [ZZKeyChainManager deleteUUID:KEY_IN_KEYCHAIN];}

 3.匯入#import "ZZUUIDManager.h"

直接擷取

 NSLog(@"---->>uuid:%@",[ZZUUIDManager getUUID]);

 

參考:iOS擷取UUID

 

擷取iOS裝置UUID

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.