Search code examples
objective-cxcodekeychainkeychainitemwrapper

login and register using keychain


I heard that using keychain is better than using NSUserDefault.
The problem is that I have no idea how to use it.
I'm trying to make a login and register for the users with all different passwords and usernames.
Can someone please demonstrate or screenshot the code for how to do it? oh and I'm using xcode 4.6


Solution

  • Here can you find apple's KeyChainItemWrapper class: https://developer.apple.com/library/ios/samplecode/GenericKeychain/Listings/Classes_KeychainItemWrapper_m.htm

    You can use it like:

     KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"YourAppLogin" accessGroup:nil];
    
    // Set the username and password using:
    [keychainItem setObject:@"password you are saving" forKey:@"Password"];
    [keychainItem setObject:@"username you are saving" forKey:@"Username"];
    
    // Get them
     NSString *password = [keychainItem objectForKey:@"Password"];
     NSString *username = [keychainItem objectForKey:@"Username"];
    
    // Delete them
    [keychainItem resetKeychainItem];