美文网首页
iOS使用KeyChain保存唯一UUID

iOS使用KeyChain保存唯一UUID

作者: 倪大头 | 来源:发表于2019-08-20 15:32 被阅读0次

把通过

[UIDevice currentDevice].identifierForVendor.UUIDString

获取到的UUID通过KeyChain保存到钥匙串中

建一个工具类:

KeyChainManager.h:

#import <Foundation/Foundation.h>
#import <Security/Security.h>

@interface KeyChainManager : NSObject

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

+ (id)load:(NSString *)service;

+ (void)delete:(NSString *)service;

@end

KeyChainManager.m:

#import "KeyChainManager.h"

@implementation KeyChainManager

+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service {
    
    return [NSMutableDictionary dictionaryWithObjectsAndKeys:
            
            (__bridge id)kSecClassGenericPassword,(__bridge id)kSecClass,
            
            service, (__bridge id)kSecAttrService,
            
            service, (__bridge id)kSecAttrAccount,
            
            (__bridge id)kSecAttrAccessibleAfterFirstUnlock,(__bridge 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 CFDictionaryRef)keychainQuery);
    
    //Add new object to search dictionary(Attention:the data format)
    
    [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge id)kSecValueData];
    
    //Add item to keychain with the search dictionary
    
    SecItemAdd((CFDictionaryRef)CFBridgingRetain(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:(__bridge id)kSecReturnData];
    
    [keychainQuery setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];
    
    CFDataRef keyData = NULL;
    
    if (SecItemCopyMatching((__bridge 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)delete:(NSString *)service {
    
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    
    SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
    
}

@end

准备一个方法读取钥匙串中的UUID:

Utils.h:

#import <Foundation/Foundation.h>

@interface Utils : NSObject

+ (NSString *)getUUID;//获取UUID

@end

Utils.m:

#import "Utils.h"
#import "KeyChainManager.h"

#define KEY_UUID_DATA @"com.datou.datounote"
#define KEY_UUID_TEXT @"UUID_TEXT"

@implementation Utils

+ (NSString *)getUUID {
    NSString *UUIDString = @"";
    NSDictionary *UUID_Dict = (NSDictionary *)[KeyChainManager load:KEY_UUID_DATA];
    if ([UUID_Dict.allKeys containsObject:KEY_UUID_TEXT]) {
        UUIDString = UUID_Dict[KEY_UUID_TEXT];
    }else {
        //没有就获取
        UUIDString = [UIDevice currentDevice].identifierForVendor.UUIDString;
        //保存
        [KeyChainManager save:KEY_UUID_DATA data:@{KEY_UUID_TEXT:UUIDString}];
    }
    return UUIDString;
}

@end

相关文章

  • iOS获取的唯一标识符

    参考文献 获取iOS设备唯一标识(UUID)并保存到钥匙串 Keychain简单理解和使用 1. UIDevice...

  • iOS使用KeyChain保存唯一UUID

    把通过 获取到的UUID通过KeyChain保存到钥匙串中 建一个工具类: KeyChainManager.h: ...

  • iOS - keychain保存UUID

    在iOS7之前可以通过系统方法直接获取到用户手机的udid 唯一标识,而之后都被苹果了,尽管后来大家又想出获取MA...

  • keychain保存UUID

    http://blog.sina.com.cn/s/blog_5971cdd00102vqgy.html

  • IOS生成唯一标识符的方案

    现在我们生成唯一的标识符的方法,主要是使用UUID,保存到Keychain里面,一边就算你删除了应用,下一次安...

  • iOS 获取设备唯一标识符

    项目中使用的是IDFA + UUID + keychain的方式。 其实个人觉得IDFV + keychain就够...

  • IOS使用 SFHFKeychainUtils 在 iOS ke

    为什么使用Keychain存储用户敏感信息 : iOS的keychain服务提供了一种安全的保存私密信息(密码,序...

  • SFHFKeychainUtils

    使用Keychain存储用户敏感信息 iOS的keychain服务提供了一种安全的保存私密信息(密码,序列号,证书...

  • 使用KeyChain保存敏感信息

    Demo KeyChain基础 为什么使用KeyChain呢?第一个原因:安全性高。在iOS中保存数据的方式有很多...

  • iOS 获取用户唯一标识

    方式使用 keyChain + uuid1.需要在xcode -> project ->target ->capa...

网友评论

      本文标题:iOS使用KeyChain保存唯一UUID

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