iOS开发 - 继udid,Mac地址等一系列唯一标识无效后,如何用KeyChain来实现设备唯一性

苹果本着为用户安全考虑的初衷导致UDID和Mac地址相继阵亡,IMEI也不例外,为了设备的唯一性,一代代开发者绞尽脑汁,后来KeyChain被他们瞄上了,终于可以继续判别社别的唯一性。

原理是利用UUID,有人说,UUID是非唯一的,很容易变化,对,没错,但是UUID绝对不会重复对吧?所以UUID我只需要获取一次,然后存入KeyChain,即使App被删除了,KeyChain中的UUID也不会被删除,如果你们公司有两款App,完全可以通过存入的key值读取出另一个App的UUID,做一些简单的交互。

具体的实现方法,网上有很多种方法,很少有全套的使用方法,博主这里重新总结了下:
先说说获取UUID的方法:

 CFUUIDRef myUUID = CFUUIDCreate( nil );
    CFStringRef uuidString = CFUUIDCreateString( nil, myUUID );
    NSString * result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));

关于实际的存储,都是属于套路的代码了,没什么可读性,直接贴出来了:
有两个管理类:

#import 

@interface LHUUIDManager : NSObject
+(void)saveUUID; //保存UUID

+(id)readUUID; //获取UUID

+(void)deleteUUID; //删除
@end


#import "LHUUIDManager.h"
#import "LHKeyChain.h"

@implementation LHUUIDManager : NSObject 
static NSString * const KEY_IN_KEYCHAIN = @"KEY_IN_KEYCHAIN";
static NSString * const KEY_IN_UUID = @"KEY_IN_UUID";

+(void)saveUUID
{
    NSMutableDictionary *myUUIDDic = [NSMutableDictionary dictionary];
    [myUUIDDic setObject:[self creatUUID] forKey:KEY_IN_UUID];
    [LHKeyChain save:KEY_IN_KEYCHAIN data:myUUIDDic];
}

+(id)readUUID
{
    NSMutableDictionary *myUUIDDic = (NSMutableDictionary *)[LHKeyChain read:KEY_IN_KEYCHAIN];
    return [myUUIDDic objectForKey:KEY_IN_UUID];
}

+(void)deleteUUID
{
    [LHKeyChain lhDelete:KEY_IN_KEYCHAIN];
}

+(NSString *)creatUUID
{
    CFUUIDRef myUUID = CFUUIDCreate( nil );
    CFStringRef uuidString = CFUUIDCreateString( nil, myUUID );
    NSString * result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));
    return result;
}
@end
#import 

@interface LHKeyChain : NSObject
+ (NSMutableDictionary *)getKeychain:(NSString *)keyChain ;

+ (void)save:(NSString *)keyChain data:(id)data;

+ (id)read:(NSString *)keyChain;

+ (void)lhDelete:(NSString *)keyChain;
@end





#import "LHKeyChain.h"

@implementation LHKeyChain
+ (NSMutableDictionary *)getKeychain:(NSString *)keyChain {
    return [NSMutableDictionary dictionaryWithObjectsAndKeys:
            (__bridge_transfer id)kSecClassGenericPassword,(__bridge_transfer id)kSecClass,
            keyChain, (__bridge_transfer id)kSecAttrService,
            keyChain, (__bridge_transfer id)kSecAttrAccount,
            (__bridge_transfer id)kSecAttrAccessibleAfterFirstUnlock,(__bridge_transfer id)kSecAttrAccessible,
            nil];
}

+ (void)save:(NSString *)keyChain data:(id)data {
    //Get search dictionary
    NSMutableDictionary *keychainQuery = [self getKeychain:keyChain];
    //Delete old item before add new item
    CFDictionaryRef aRef = (__bridge_retained CFDictionaryRef)keychainQuery;
    SecItemDelete(aRef);
    //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(aRef, NULL);
}

+ (id)read:(NSString *)keyChain {
    id ret = nil;
    NSMutableDictionary *keychainQuery = [self getKeychain:keyChain];
    //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: %@", keyChain, e);
        } @finally {
        }
    }
    return ret;
}

+ (void)lhDelete:(NSString *)keyChain {
    NSMutableDictionary *keychainQuery = [self getKeychain:keyChain];
    SecItemDelete((__bridge_retained CFDictionaryRef)keychainQuery);
}
@end

到这里是所有关键代码,放上下载地址:点击下载

最后,忘了件重要的事情,要在xcode中启用KeyChain:
iOS开发 - 继udid,Mac地址等一系列唯一标识无效后,如何用KeyChain来实现设备唯一性_第1张图片
当显示为ON的时候才能使用KeyChain。

你可能感兴趣的:(iOS日常开发)