简体   繁体   English

保存用户名并通过Objective C iPhone

[英]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. 如果您在NSUserDefaults保存用户名和/或密码, NSUserDefaults它们保存为明文,这本质上是不安全的。

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. 互联网上有很多关于如何在iPhone上使用钥匙串的例子,包括在你的代码中使用的简单包装器。 For example, here's some pretty good code on Github that makes it quite easy: 例如,这里有一些非常好的Github代码,它非常简单:

http://github.com/ldandersen/scifihifi-iphone/tree/master/security 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) ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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