多个APP之间共享存储数据-iOS keyChain

多个APP之间共享存储数据-iOS keyChain_第1张图片
深沉的眼神.jpg

最近公司有个需求是一个app根据一些信息生成一个唯一码,后边安装的app也是用这一个唯一码。即多个app公用同一个数据。
一般数据存储就是保存在沙盒之类的地方,但是沙盒删除之后本地的数据就会被删除,不符合需求。
经过调研,发现可以用keychain来实现,那么如何使用keyChain呢?

下边是keyChain的方法:

- (NSMutableDictionary *)getKeychainQuery:(NSString *)service
{
    return [NSMutableDictionary dictionaryWithObjectsAndKeys:(id)kSecClassGenericPassword, (id)kSecClass, service, (id)kSecAttrService, service, (id)kSecAttrAccount, (id)kSecAttrAccessibleAfterFirstUnlock, (id)kSecAttrAccessible, nil];
}
//保存interface
- (void)saveToKeychain:(NSString *)service data:(id)data
{
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    SecItemDelete((CFDictionaryRef)keychainQuery);
    [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];
    SecItemAdd((CFDictionaryRef)keychainQuery, NULL);
}
//获取interface
- (id)queryForKeychain:(NSString *)service
{
    id ret = nil;
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    [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 *exception) {
            ret = @"";
        }
    }
    
    if (keyData) {
        CFRelease(keyData);
    }
    return ret;
}
//删除
- (void)deleteForKeychain:(NSString *)service
{
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    SecItemDelete((CFDictionaryRef)keychainQuery);
}

- (NSString *)keyChainStartkey
{
   
    return @"keyChainStartkey";
}

怎么调用接口呢?
NSString *const keychain_password_suffix = @".password";
NSString *const keychain_username_password_suffix = @".usernamepassword";
#pragma mark - keychain

- (NSString *)keychain_usernamepassword
{
    NSString *keychain_usernamepassword = [self.keyChain. keyChainStartkey stringByAppendingString:keychain_username_password_suffix];
    return keychain_usernamepassword;
}

- (NSString *)keychain_passwordBaseName
{
    return [self.keyChain.bundleId stringByAppendingFormat:@"%@", keychain_password_suffix];
}

- (void)saveFingerToKeychain:(NSString *)info
{
    NSDictionary *info_dic = @{[self keychain_passwordBaseName]: info};
    [self.keyChain saveToKeychain:[self keychain_usernamepassword] data:info_dic];
}

- (NSString *)getFingerForKeychain
{
    NSDictionary *info_dic = [self.keyChain queryForKeychain:[self keychain_usernamepassword]];
    if (info_dic) {
        NSString *info_str = [info_dic objectForKey:[self keychain_passwordBaseName]];
        return [self noNullString:info_str];
    }
    return @"";
}

- (void)clearFingerForKeychain
{
    [self.keyChain deleteForKeychain:[self keychain_usernamepassword]];
}

这个时候基本的代码调用已经完成,但是只有代码还是不行的,还需要配置keyChainGroup,不然的话即使摘到key也获取不到共享的数据的。
下边来说下配置:

多个APP之间共享存储数据-iOS keyChain_第2张图片
配置.png

到此我们已经可以使用keyChain来实现多个app之间的数据共享。

你可能感兴趣的:(多个APP之间共享存储数据-iOS keyChain)