Reputation:
I'm storing username and password in keychain. Here's my code
To save and fetch
I'm trying to store multiple username and passwords using service
When login button clicked
[keychain setObject:@"facebookLogin" forKey:(id)kSecAttrService];
[keychain setObject:username forKey:(id)kSecAttrAccount];
[keychain setObject:password forKey:(id)kSecValueData];
[keychain setObject:@"appLogin" forKey:(id)kSecAttrService];
[keychain setObject:username forKey:(id)kSecAttrAccount];
[keychain setObject:password forKey:(id)kSecValueData];
When show button clicked
NSLog(@"%@", [keychain objectForKey:(id)kSecAttrAccount]);
NSLog(@"%@", [keychain objectForKey:(id)kSecValueData]);
How to fetch the username and password which is stored for facebook login and app login?
And I want to store cookie value.
[keychain setObject:@"cookieString" forKey:(id)kSecAttrService];
[keychain setObject:myCookie forKey:(id)kSecValueData];
Is this is a right way to store the values?
Any help appreciated...
Upvotes: 0
Views: 1314
Reputation: 3465
This should work:
Saving an item to Keychain:
KeychainItemWrapper *login = [[KeychainItemWrapper alloc] initWithIdentifier:@"login" accessGroup:nil];
[wrapper setObject:username1 forKey:(__bridge id)kSecAttrAccount];
[wrapper setObject:password1 forKey:(__bridge id)kSecValueData];
[wrapper setObject:kSecAttrAccessibleAlwaysThisDeviceOnly forKey:(__bridge id)kSecAttrAccessible];
KeychainItemWrapper *facebookLogin = [[KeychainItemWrapper alloc] initWithIdentifier:@"facebooklogin" accessGroup:nil];
[wrapper setObject:username2 forKey:(__bridge id)kSecAttrAccount];
[wrapper setObject:password2 forKey:(__bridge id)kSecValueData];
[wrapper setObject:kSecAttrAccessibleAlwaysThisDeviceOnly forKey:(__bridge id)kSecAttrAccessible];
Retrieving an item from Keychain:
// Create respective KeychainItemWrapper objects with identifiers you want to retrieve from
KeychainItemWrapper *login = [[KeychainItemWrapper alloc] initWithIdentifier:@"login" accessGroup:nil];
NSData *passwordData1 = [login objectForKey:(__bridge id)kSecValueData];
NSString *passwordString1 = [[NSString alloc] initWithData:passwordData1 encoding:NSUTF8StringEncoding];
NSLog(@"%@", passwordString1);
KeychainItemWrapper *facebookLogin = [[KeychainItemWrapper alloc] initWithIdentifier:@"facebooklogin" accessGroup:nil];
NSData *passwordData2 = [facebookLogin objectForKey:(__bridge id)kSecValueData];
NSString *passwordString2 = [[NSString alloc] initWithData:passwordData2 encoding:NSUTF8StringEncoding];
NSLog(@"%@", passwordString2);
Use initWithIdentifier
to store items for different categories as you like. You can choose to include or omit kSecAttrService
as you have used in your question. initWithIdentifier
is enough and good.
NOTE
Here, kSecAttrAccessible
is really important, 'cause a keychain item has various levels of security of access to it. If your phone has touchId/passcode enabled, use this constant(kSecAttrAccessibleAlwaysThisDeviceOnly) to access keychain item from anywhere, i.e., without unlocking your device.
Upvotes: 1
Reputation: 399
Since iOS 10.3 beta 2 an app's keychain entries will be deleted when the app gets uninstalled. Confirmed in the Apple Developer Forums.
Upvotes: 0