How To Get Keychain Libraries Working On iOS 10

Alexander Paterson
Posted about 6 years ago
2 minutes
How To Get Keychain Libraries Working On iOS 10
iOS 10 broke both keychain-swift and react-native-keychain

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")

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:

Enable Keychain Sharing

This will generate a .entitlements file for you, with the following content:

Entitlements File

If you need to manually generate this file, you can do it with New File... > Property List, giving it a .entitlements extension.

New File

Property List

You should then also make sure it's referenced properly in your Build Settings:

Build Settings Signing Entitlements

It's not working

If you're seeing something that looks like this:

Add the Keychain Sharing Feature To Your App ID

Try disabling it, restarting Xcode, and enabling it again. Maybe do a clean with CMD+SHIFT+K at some point, too.