获取数据时:
KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"LoginData" accessGroup:nil];
NSString *user = [[NSUserDefaults standardUserDefaults] valueForKey:@"ACC"];
NSString *pass = [keychain objectForKey:CFBridgingRelease(kSecAttrAccount)];
保存数据时:
KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"LoginData" accessGroup:nil];
[[NSUserDefaults standardUserDefaults] setObject:[jsonResult objectForKey:@"user"] forKey:@"ACC"];
[keychain setObject:[jsonResult objectForKey:@"token"] forKey:CFBridgingRelease(kSecAttrAccount)];
我正在重新使用几年前使用的代码。然而,现在我遇到了一些问题,其中一部分无法正常工作。
值得一提的是,我使用的是KeyChainItemWrapper版本:1.2(Objective-c(
[[NSUserDefaults standardUserDefaults] valueForKey:@"ACC"]
工作正常,并且已保存,但是存储CFBridgingRelease(kSecAttrAccount)
的值不会保存。当运行fetch代码时,在保存代码之后,我只得到(null)
。
我使用xcode 8.3.1和模拟器版本10,运行iPhone 7版本10.3。
最新的IDE版本&iOS版本是唯一改变的东西
要将数据保存和检索到密钥链,请确保使用苹果默认的ARC版本。
保存到密钥链:-
KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"Your identifire" accessGroup:nil];
[keychainItem setObject:appleUserId forKey:(id)CFBridgingRelease(kSecAttrAccount)];
从密钥链检索数据:-
KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"Your identifire" accessGroup:nil];
NSString *savedUserID = [keychainItem objectForKey:(id)CFBridgingRelease(kSecAttrAccount)];
欲了解更多信息,请查看官方链接:
https://developer.apple.com/documentation/foundation/1587932-cfbridgingrelease
https://gist.github.com/dhoerl/1170641