码迷,mamicode.com
首页 > 移动开发 > 详细

获取iOS设备UUID

时间:2018-04-26 18:22:16      阅读:286      评论:0      收藏:0      [点我收藏+]

标签: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 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

标签:account   div   emc   man   attr   eric   manage   ecc   test   

原文地址:https://www.cnblogs.com/iness/p/8953687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!