iOS 設備唯一UDID獲取

使用的方法以及封裝的工具類, 在應用里使用使用keyChain,我們需要導入Security.framework。創建一個獲取唯一標識的工具類:

XMGKeychain.h 文件中就這一個方法

  @interface XMGKeychain : NSObject

  +(NSString *)getDeviceIDInKeychain;

  @end

XMGKeychain.m 文件中如下

NSString * const KEY_UDID_INSTEAD = @"com.myapp.udid.test";

@implementation XMGKeychain


+(NSString *)getDeviceIDInKeychain
{
NSString *getUDIDInKeychain = (NSString *)[self load:KEY_UDID_INSTEAD];
NSLog(@"從keychain中獲取到的 UDID_INSTEAD %@",getUDIDInKeychain);
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);
    NSLog(@"\n \n \n _____重新存儲 UUID _____\n \n \n  %@",result);
    [self save:KEY_UDID_INSTEAD data:result];
    getUDIDInKeychain = (NSString *)[self load:KEY_UDID_INSTEAD];
}
NSLog(@"最終 ———— UDID_INSTEAD %@",getUDIDInKeychain);
return getUDIDInKeychain;
}

#pragma mark - private

  + (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:(__bridge NSData *)keyData];
    } @catch (NSException *e) {
        NSLog(@"Unarchive of %@ failed: %@", service, e);
    } @finally {
    }
}
if (keyData)
    CFRelease(keyData);
return ret;
}

+ (void)delete:(NSString *)service {
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
SecItemDelete((CFDictionaryRef)keychainQuery);
}

@end
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容