美文网首页
UUID获取及写入Keychain

UUID获取及写入Keychain

作者: 小弱鸡 | 来源:发表于2016-12-05 13:53 被阅读67次

    在开发过程中,很多时候需要查看该用户是上传唯一识别标志,安卓设备可以设定上传IMEI。但苹果设备已经不支持获取IMEI,作为替代我们使用UUID来作唯一标示符。
    ****一、uuid类和keychain类****

    #import <Foundation/Foundation.h>
    
    @interface HAUUID : NSObject
    /**
     *  获取uuid
     *
     *  @param block 返回uuid和是否初装
     */
    + (void)getUUIDCompletionBlock:(void(^)(NSString*UUIDString,BOOL firstInstall))block;
    @end
    
    #import "HAUUID.h"
    #import "HAKeyChainStore.h"
    
    #define  KEY_USERNAME_PASSWORD @""
    #define  KEY_USERNAME @"com.company.app.username"
    #define  KEY_PASSWORD @"com.company.app.password"
    @implementation HAUUID
    + (void)getUUIDCompletionBlock:(void(^)(NSString*UUIDString,BOOL firstInstall))block{
        NSString * strUUID = (NSString *)[HAKeyChainStore load:KEY_USERNAME_PASSWORD];
        BOOL installState  =NO;
        //首次执行该方法时,uuid为空
        if ([strUUID isEqualToString:@""] || !strUUID)
        {
            installState =YES;//初装
            //生成一个uuid的方法
            CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
            
            strUUID = (NSString *)CFBridgingRelease(CFUUIDCreateString (kCFAllocatorDefault,uuidRef));
            
            //将该uuid保存到keychain
            [HAKeyChainStore save:KEY_USERNAME_PASSWORD data:strUUID];
            
        }
        block(strUUID,installState);
    }
    
    
    @end
    
    #import <Foundation/Foundation.h>
    
    @interface HAKeyChainStore : NSObject
    
    + (void)save:(NSString *)service data:(id)data;
    + (id)load:(NSString *)service;
    + (void)deleteKeyData:(NSString *)service;
    
    @end
    
    #import "HAKeyChainStore.h"
    
    @implementation HAKeyChainStore
    
    
    + (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)deleteKeyData:(NSString *)service {
        NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
        SecItemDelete((CFDictionaryRef)keychainQuery);
    }
    
    @end
    

    ****二、生成Entitlements文件****

    左侧的目录会自动生成Entitlements文件,不需要自己创建了。
    也就是说,Bundle Identifier、Keychain Sharing的Keychain Groups、Entitlements文件的Keychain Access Groups的第一个元素,它们要保持上图所示的一致性。

    相关文章

      网友评论

          本文标题:UUID获取及写入Keychain

          本文链接:https://www.haomeiwen.com/subject/qxpnettx.html