iOS: How to store username/password within an app?

前端 未结 14 1157
独厮守ぢ
独厮守ぢ 2020-11-22 06:18

I have a login-screen in my iOS app. The username and password will be saved in the NSUserDefaults and be loaded into the login-screen again when you enter the

相关标签:
14条回答
  • 2020-11-22 06:32

    The following should work just fine:

    KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"YourAppLogin" accessGroup:nil];
    [keychainItem setObject:@"password you are saving" forKey:kSecValueData]; 
    [keychainItem setObject:@"username you are saving" forKey:kSecAttrAccount];
    
    0 讨论(0)
  • 2020-11-22 06:35

    To update this question:

    For those using Swift checkout this drag and drop swift implementation by Mihai Costea supporting access groups:

    https://github.com/macostea/KeychainItemWrapper.swift/blob/master/KeychainItemWrapper.swift

    Before using the keychain: consider twice before storing passwords. In many cases storing an authentication token (such as a persistence session id) and the email or account name might be enough. You can easily invalidate authentication tokens to block unauthorized access, requiring the user to login again on the compromised device but not requiring reset password and having to login again on all devices (we are not only using Apple are we?).

    0 讨论(0)
  • 2020-11-22 06:41

    For swift you can use this library:

    https://github.com/jrendel/SwiftKeychainWrapper

    It supports all versions of swift.

    0 讨论(0)
  • 2020-11-22 06:42

    checkout this sample code i tried first the apple's wrapper from the sample code but this is much simpler for me

    0 讨论(0)
  • 2020-11-22 06:45

    But, now you can go for NURLCredential instead of keychain wrapper. It does what we need to do.

    0 讨论(0)
  • 2020-11-22 06:49

    You can simply use NSURLCredential, it will save both username and password in the keychain in just two lines of code.

    See my detailed answer.

    0 讨论(0)
提交回复
热议问题