views:

694

answers:

4

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.

+10  A: 

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

Ghommey
is the password returned in plaintext?
pxl
when I add that class to my project I get a bunch of weird errors such as "_kSecAttrAccount", referenced from: _kSecAttrAccount$non_lazy_ptr in SFHFKeychainUtils.o"_SecItemDelete", referenced from: +[SFHFKeychainUtils deleteItemForUsername:andServiceName:error:] in SFHFKeychainUtils.o"_kSecReturnAttributes", referenced from: _kSecReturnAttributes$non_lazy_ptr in SFHFKeychainUtils.o"_kSecClass", referenced from: _kSecClass$non_lazy_ptr in SFHFKeychainUtils.o"_kSecClassGenericPassword", referenced from: _kSecClassGenericPassword$non_lazy_ptr in SFHFKeychainUtils.o
Jason
+4  A: 

The keychain is what you are looking for.

Nikolai Ruhe
That link is for the Mac OS X keychain. Here's the link for the iPhone version (they're slightly different): http://developer.apple.com/IPhone/library/documentation/Security/Conceptual/keychainServConcepts/02concepts/concepts.html
Dave DeLong
Oh, thanks. Fixed that.
Nikolai Ruhe
+2  A: 

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

FigBug
+1  A: 

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

slf