user393273
user393273

Reputation: 1438

Save Username & Pass Objective C iPhone

i have a textfield,

Number and Password

i have no clue how to save these settings and then read them as the app starts to check if they have been set or not.

Thanks

Mason

Upvotes: 5

Views: 3133

Answers (2)

Steve
Steve

Reputation: 31652

To Save:

[[NSUserDefaults standardUserDefaults] setValue:_email forKey:@"email"];
[[NSUserDefaults standardUserDefaults] setValue:_password forKey:@"password"];
[[NSUserDefaults standardUserDefaults] synchronize];

To Read:

_email = [[NSUserDefaults standardUserDefaults] stringForKey:@"email"];
_password = [[NSUserDefaults standardUserDefaults] stringForKey:@"password"];

In your case:

[[NSUserDefaults standardUserDefaults] setValue:Number.text forKey:@"Number"];

And:

NSString * number = [[NSUserDefaults standardUserDefaults] stringForKey:@"Number"];

The "Key" is usually hard-coded and works like a "variable name" for things in storage (typical name/value pairs like a dictionary).

To check if the value has been set or not; after you read it:

if (number) ...

Upvotes: 5

Dave DeLong
Dave DeLong

Reputation: 243156

Sensitive information should be stored in the keychain, a cryptographically secure location on the device. If you save the username and/or password in NSUserDefaults, you're saving them as plaintext, which is inherently insecure.

There're are plenty of examples on the internet of how to use the keychain on the iPhone, include simple wrappers to use in your code. For example, here's some pretty good code on Github that makes it quite easy:

http://github.com/ldandersen/scifihifi-iphone/tree/master/security

Upvotes: 13

Related Questions