简体   繁体   English

首次启动应用时如何获取和存储用户名和密码?

[英]How to get and store Username and password when app is launched for the first time?

I have a tabbar app and I want to add login window that will show just for the first time the app is launched.我有一个标签栏应用程序,我想添加登录 window,它将仅在应用程序首次启动时显示。 and want username and password to be hard coded in the app.并希望在应用程序中硬编码用户名和密码。 can anyone help me with this please.任何人都可以帮我解决这个问题。

You probably want to use KeyChain to store the information.您可能想使用 KeyChain 来存储信息。 Keychain is a secure way to store passwords since the storage values are encrypted.钥匙串是存储密码的安全方式,因为存储值是加密的。

Apple also has a sample project, GenericKeychain : Apple 还有一个示例项目GenericKeychain

This sample shows how to add, query for, remove, and update a keychain item of generic class type.此示例演示如何添加、查询、删除和更新通用 class 类型的钥匙串项。 Also demonstrates the use of shared keychain items.还演示了共享钥匙串项的使用。 All classes exhibit very similar behavior so the included examples will scale to the other classes of Keychain Item: Internet Password, Certificate, Key, and Identity.所有类都表现出非常相似的行为,因此包含的示例将扩展到 Keychain 项的其他类:Internet 密码、证书、密钥和身份。

Another example of how to implement this using SFHFKeychainUtils is available here . 此处提供了如何使用 SFHFKeychainUtils 实现此功能的另一个示例。

if your wanting to save the user name and password in a secure way, store that in the devices key chain - apple provide a wrapper class for this to make it easier to use.如果您想以安全的方式保存用户名和密码,请将其存储在设备密钥链中 - 苹果为此提供了一个包装器 class 以使其更易于使用。 you can also have a value stored in the device NSUserDefaults that can be set when the user has ran the app for the first time and this value can be evaulated on subsequent logins to see if it is required to show the login page or go directly to the relevant view您还可以在设备 NSUserDefaults 中存储一个值,该值可以在用户第一次运行应用程序时设置,并且可以在后续登录时评估该值,以查看是否需要显示登录页面或 go 直接到相关观点

keychain stuff here -ME钥匙链的东西在这里-ME

NSUserDefault stuff here - ME NSUserDefault 的东西在这里 -

I like SFHFKeychainUtils, but if you want it to be removed upon deletion (I see this in another comment), you'll need to store it with the app in the documents directory, preferably with NSUserDefaults:我喜欢 SFHFKeychainUtils,但如果您希望在删除时将其删除(我在另一条评论中看到),您需要将其与应用程序一起存储在文档目录中,最好使用 NSUserDefaults:

[[NSUserDefaults standardUserDefaults] setObject:@"myuser" forKey:@"username"];
[[NSUserDefaults standardUserDefaults] synchronize];

[[NSUserDefaults standardUserDefaults] stringForKey:@"username"];

This does not manage security but will delete along with the app.这不会管理安全性,但会与应用程序一起删除。 You can check for the username and if it does not exist or if login fails upon use, bring up your login view as a modal dialog.您可以检查用户名,如果它不存在或登录失败,则将您的登录视图显示为模式对话框。

Consider using the keychain instead.考虑改用钥匙串。 Even though the password will stay when the app is deleted, it will be encrypted.即使删除应用程序时密码会保留,它也会被加密。

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

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