0

I need to store a persistent remember token (a string) between app launches and device restarts. The token will be provided by a server once my user logs into the app and its back end service (which is already built). Specifically, I need to set up a persistent data placeholder for the remember token but I don't ever want the code to actually set the value of that placeholder.

On the one hand, it seemed like NSUserDefaults (now called UserDefaults) was a simple way to do this, but after reading the documentation, it doesn't seem like that was the intention of the feature. All the documentation I've see shows setting it up by assigning a value to a key. I definitely don't want to ever have the app assign a value to that key.

What's the simplest way to do this?

4

1 回答 1

0

在考虑安全性的同时实现这一目标的最简单方法是使用钥匙串。存储在 Keychain 中的数据使用密钥加密,例如在设置 Touch ID 支持时。

您永远不应该将 UserDefaults 用于此类任务。原因是 UserDefaults 由简单的、未加密的 .plist 文件支持,该文件是捆绑包的一部分,任何可以访问您的 ipa 的人(例如,任何拥有越狱设备的人)都可以或多或少地轻松查看。

KeychainAccess API 是用 Objective-C 编写的,但是有许多使用 Swift 封装它的包装器。您可以在https://github.com/kishikawakatsumi/KeychainAccess上使用它

于 2017-04-28T21:02:28.893 回答