iOS获取UUID

转自:《iOS获取设备的唯一标识的方法总结以及最好的方法》

参考:《获取iOS设备唯一标识》

总结一下:

1.代码采用CFUUID+KeyChain的实现方式。

2.CFUUID、IDFA、IDFV都是可变的(其它方法,比如苹果自带的UUID和MAC地址基本上被禁止使用),但是IDFA和IDFV不一定都能获取到(iOS系统不同版本不一定支持),所以直接选择了CFUUID(iOS2.0就开始支持了)。

PS:

3.代码中使用stringByReplacingOccurrencesOfString将间隔符-去掉了(原来是这种:CAE1CC3A-92C1-45A5-9F55-932264020358,去掉后变为CAE1CC3A92C145A59F55932264020358)。

4.SdkMgr为自定义类名,根据需要自行修改。

NSString * const KEY_UDID_INSTEAD = @"com.app.uuid.test";

+(NSString *)getDeviceUUID{
    NSString *getUDIDInKeychain = (NSString *)[SdkMgr load:KEY_UDID_INSTEAD];
    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);
        
        //去掉横线,保留32位长度
        NSString *rst = [result stringByReplacingOccurrencesOfString:@"-" withString:@""];
        [SdkMgr save:KEY_UDID_INSTEAD data:rst];
        getUDIDInKeychain = (NSString *)[SdkMgr load:KEY_UDID_INSTEAD];
    }
    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:(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



原文地址:https://www.cnblogs.com/chevin/p/8213877.html