简体   繁体   中英

iOS, Keychain kSecAttrAccount not saving

When fetching the data:

KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"LoginData" accessGroup:nil];
NSString *user = [[NSUserDefaults standardUserDefaults] valueForKey:@"ACC"];
NSString *pass = [keychain objectForKey:CFBridgingRelease(kSecAttrAccount)];

When saving the data:

KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"LoginData" accessGroup:nil];
[[NSUserDefaults standardUserDefaults] setObject:[jsonResult objectForKey:@"user"] forKey:@"ACC"];
[keychain setObject:[jsonResult objectForKey:@"token"] forKey:CFBridgingRelease(kSecAttrAccount)];

I'm re-using code i've used a couple years ago. However now i'm experiancing issues with part of it not working correctly.

For what it's worth, i'm using the KeyChainItemWrapper Version: 1.2 ( Objective-c )

The [[NSUserDefaults standardUserDefaults] valueForKey:@"ACC"] works fine, and is saved, however storing a value for CFBridgingRelease(kSecAttrAccount) doesn't save. When running the fetch code, after the save code, i only get (null) .

I'm using xcode 8.3.1 with simulator version 10, running iPhone 7 version 10.3.

The newest IDE version & iOS version is the only thing that has changed

For Saving and Retrieving Data to Key chain make sure to use the Apple default ARC release.

Save To key chain:-

KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"Your identifire" accessGroup:nil];
  [keychainItem setObject:appleUserId forKey:(id)CFBridgingRelease(kSecAttrAccount)];

Retrieve data from the key chain:-

KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"Your identifire" accessGroup:nil];
        
        NSString *savedUserID = [keychainItem objectForKey:(id)CFBridgingRelease(kSecAttrAccount)];

For More info check the official link:

https://developer.apple.com/documentation/foundation/1587932-cfbridgingrelease

https://gist.github.com/dhoerl/1170641

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM