美文网首页
keychain保存App留存的信息

keychain保存App留存的信息

作者: 辛乐 | 来源:发表于2021-03-01 10:20 被阅读0次

    参考转载:iOS开发之如何在用户删除应用后保持一些数据: https://blog.csdn.net/u014410695/article/details/50623591

    KeychainManager创建使用
    .h

    +(KeychainManager*)default;
    /**根据字典存储对象到钥匙串*/
    - (void)save:(NSString *)service data:(id)data;
    /**根据字典读取钥匙串里的对象*/
    - (id)load:(NSString *)service;
    /**删除钥匙串里的数据*/
    - (void)delete:(NSString *)service;
    

    .m

    +(KeychainManager*)default
    {
        static KeychainManager *keychainManager = nil;
        if(keychainManager == nil)
        {
            keychainManager = [[KeychainManager alloc] init];
        }
        return keychainManager;
    }
    - (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 requiringSecureCoding:YES error:nil] 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];
                ret = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSObject class] fromData:(__bridge NSData *)keyData error:nil];
                
            } @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((CFDictionaryRef)keychainQuery);
    }
    

    相关文章

      网友评论

          本文标题:keychain保存App留存的信息

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