简体   繁体   中英

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

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) ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM