views:

78

answers:

2

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

A: 

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) ...
Steve
so for the textfield named Number thats an iboutlet i would use this: [[NSUserDefaults standardUserDefaults] setValue:_number forKey:Number.text];[[NSUserDefaults standardUserDefaults] setValue:_password forKey:Password.text];[[NSUserDefaults standardUserDefaults] synchronize];
Usually the "Key" is hard-coded... this is sorta like your "variable name" for the value being stored... like this...[[NSUserDefaults standardUserDefaults] setValue:Number.text forKey:@"Number"];Does that make sense?
Steve
This will work, but it's insecure. You should be storing sensitive information in the Keychain.
Dave DeLong
ohh i get you i need to wait 3 minutes to accept answer soo im not ingnorring btw :)
@dave I suppose, but I don't think you can display/edit keychain information from the "settings" app... (i could easily be mistaken here)... so if you want to display the username in the settings app you have to go with user defaults... plus i'm not really clear on how the keychain is of value here... settings on the phone are encrypted anyways, and if the user is concerned about that stuff they should use a passcode lock on their phone. David, could you post an answer using the Keychain? I'm interested.
Steve
@sw12345 you can easily crack open someone's iPhone backups and strip out passwords out of the plaintext NSUserDefaults file. Use the keychain. **this is what it's for**
Dave DeLong
the security is not a must, not really top priority if they did have the password they couldnt do much. but yeh if you could post an example for some of us to learn off it would be nice
im getting undeclared error
error after i read, can you explain abit please
thanks for all your help i will get this to work by tomorrow hehe.thanks thanks thanks
ugh, casting `[NSNull null]` to an `NSString*`? That's *terrible*. You *really* don't need to do that, especially since it's wrong. The documentation clearly states that `stringForKey:` returns `nil` (which is *very* different from `[NSNull null]`) if the value doesn't exist.
Dave DeLong
ah, yeah, dave is right... sorry about that. you can just check: if (number)... Thanks for the correction dave!
Steve
+3  A: 

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

Dave DeLong
Better answer than mine. Thanks for the info. Of course, any developer should determine how much security is needed for their own application's purposes, and understand the drawbacks of using the keychain (simulator support, settings app support, added complexity and dev time).
Steve