获取iOS设备UUID

Posted iness

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取iOS设备UUID相关的知识,希望对你有一定的参考价值。

获取到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 ZZUUIDManager

static 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的主要内容,如果未能解决你的问题,请参考以下文章

IOS - - 获取设备的UUID

离子+电容|在 iOS 设备中获取设备 UUID

如何在 iOS 7 中使用 iBeacons 获取设备详细信息,如 UUID 或设备名称

iOS获取设备卸载后不变的UUID

iOS设备UUID的获取方法(不变的唯一值)

iOS开发-获取设备唯一标识符(UUID)