I've been telling students to use a Swift package called keychain-swift, and a node module called react-native-keychain for a while now. iOS 10 has broken the default behavior of both of these packages though, and now they require a bit of extra setup.
For example, installing KeychainSwift and running the following code:
let keychain = KeychainSwift()
keychain.set("hello world", forKey: "mykey")
let str = keychain.get("mykey")
print(str)
This doesn't print "hello world", it simply prints nil. Or, with react-native-keychain, performing actions will give you the following error (big red screen in the simulator):
Error: {
code = "-34018";
domain = NSOSStatusErrorDomain;
message = "The operation couldn\U2019t be completed. (OSStatus error -34018.)";
}
To fix this, open your xcode project file, and under Capabilities, enable Keychain Sharing:
This will generate a .entitlements file for you, with the following content:
If you need to manually generate this file, you can do it with New File... > Property List, giving it a .entitlements extension.
You should then also make sure it's referenced properly in your Build Settings:
It's not working
If you're seeing something that looks like this:
Try disabling it, restarting Xcode, and enabling it again. Maybe do a clean with CMD+SHIFT+K at some point, too.