Save Username & Pass Objective C iPhone
i have a textfield,
Number and Pa开发者_如何转开发ssword
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
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
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) ...
精彩评论