What is the best practice to store username and pa

2020-02-23 08:47发布

问题:

Is there a best practice way to store username and password on the iPhone? I am looking for something that is obviously secure but will also keep the info between app updates.

回答1:

Use the Apple Keychain.

+ (NSString *) getPasswordForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error;

+ (void) storeUsername: (NSString *) username andPassword: (NSString *) password forServiceName: (NSString *) serviceName updateExisting: (BOOL) updateExisting error: (NSError **) error;

The first method allows you to request the password associated with an existing username for a particular service name (I’ve just been using the name of my app as a service name). The second allows you to store a username/password/service name combo, and allows you to specify whether or not the appropriate keychain item should be updated with the provided password if an existing one is found that matches the username and service name pair. The last parameter of each is a reference to an NSError object which will contain lower level error information if something goes wrong (and be nil if it does not).

For more information see his blog



回答2:

The keychain is what you are looking for.



回答3:

Use the Keychain, here is some code to make it very easy. Works on the device and simulator.



回答4:

See the Generic Keychain example source. That's the way to go IMHO