Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 397
user393273 Avatar asked Jul 17 '10 21:07

user393273


2 Answers

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

like image 120
Dave DeLong Avatar answered Sep 30 '22 21:09

Dave DeLong


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) ...
like image 20
Steve Avatar answered Sep 30 '22 21:09

Steve