简体   繁体   English

关闭ios应用程序时如何使用钥匙串?

[英]How can I use keychain when I close my ios app?

I'm developing an iOS app and want user only type username:password pair only once (unless he logs out). 我正在开发一个iOS应用,希望用户仅输入一次username:password pair(除非他注销)。 Currently I use keychain-swift framework for storing emails/passwords. 目前,我使用钥匙串交换框架来存储电子邮件/密码。

I basically run: 我基本上运行:

let keychain = KeychainSwift()
keychain.set("johndoe", forKey: "my key") // on a successful login
keychain.get("my key")

When I run my app in a simulator I have to type password all the time (ie, it doesn't look like it saves the password in keychain between the sessions). 当我在模拟器中运行我的应用程序时,我必须一直输入密码(即,看起来好像它不会在会话之间的钥匙串中保存密码)。

Is it expected? 是预期的吗? What framework will allow me to save the data even when I close the app such that a user won't have to type username:password pairs every time to sign in? 什么框架可以让我保存数据,即使我关闭应用程序也可以使用户不必每次登录都键入username:password对?

I have never used KeychainSwift but at a guess you could do something like: 我从没使用过KeychainSwift但猜测您可以执行以下操作:

let keychain = KeychainSwift(keyPrefix: "com.Daniel.myIOSapp.")
keychain.set("johndoe", forKey: "username")
keychain.set("where is jane", forKey: "password")

which will create two "generic password" keychain items com.Daniel.myIOSapp.username and com.Daniel.myIOSapp.password and the associated values. 这将创建两个“通用密码”钥匙串项com.Daniel.myIOSapp.usernamecom.Daniel.myIOSapp.password以及关联的值。

You normally store the username/password pair as a single keychain item. 通常,您将用户名/密码对存储为单个钥匙串项。 You can do that with KeychainSwift using something like: 您可以使用KeychainSwift使用类似的方法来做到这一点:

keychain.set("where is jane", forKey: "johndoe")

which creates a single generic password item in the keychain and you probably want to store "johndoe" in your preferences under a suitable key. 它会在钥匙串中创建一个通用密码项,并且您可能希望在首选项中的适当密钥下存储"johndoe"

HTH HTH

Haven't used this framework myself, but from looking at the code it appears that it will save, and therefore be accessible after app is restarted. 我自己没有使用过此框架,但是通过查看代码,它似乎可以保存,因此在重新启动应用程序后可以访问。

You might want to confirm the set function is working properly. 您可能要确认set功能是否正常工作。 From the readme here: 从这里的自述文件:

https://github.com/evgenyneu/keychain-swift/blob/master/README.md https://github.com/evgenyneu/keychain-swift/blob/master/README.md

...it states: ...它指出:

Check if operation was successful 检查操作是否成功

One can verify if set, delete and clear methods finished successfully by checking their return values. 可以通过检查它们的返回值来验证设置,删除和清除方法是否成功完成。 Those methods return true on success and false on error. 这些方法成功返回true,错误返回false。

if keychain.set("hello world", forKey: "my key") { // Keychain item is saved successfully } else { // Report error } 如果keychain.set(“ hello world”,forKey:“我的钥匙”){//钥匙串项保存成功} else {//报告错误}

Also, just to confirm, the get function should read: 同样,为了确认,get函数应显示为:

let myKey = keychain.get("my key")

...where myKey is put into your text field(s). ...将myKey放在您的文本字段中。

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

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