iOS Keychain: Sharing data between apps

Over the last week I have been playing around with the Keychain that comes with iOS to share data between multiple apps. I stumbled upon UICKeyChainStore which makes using the Keychain super simple.

There are plenty of posts out there that cover simply using the Keychain but not that many cover sharing data between apps.  So that is exactly what this post will be about.  Sharing data between your iOS apps via the Keychain.

Creating your first app that will write to the Keychain

Setting up your project
  1. Add the Security.framework to your project
  2. Download UICKeyChainStore and add the files to your project
  3. Create a new Entitlements.plist file and specify a keychain group you want to write to in this case we will just specify our apps bundle identifier.
    • $(AppIdentifierPrefix)$(CFBundleIdentifier) this will be resolved to your app Identifier prefix (aka bundle seed identifier) and your bundle identifier which is from your Info.plist.
    • For this example assume the value is $(AppIdentifierPrefix).com.my.firstapp
  4. Browse to your current app Targets Summary tab. Scroll down the bottom and you will see a section called Entitlements specify your Entitlements.plist file and make sure Enable Entitlements is checked.
Saving data and reading data from the Keychain

Now all you need to do is start using the UICKeyChainStore and you’ll be saving data to the Keychain.

[UICKeyChainStore setString:@"supersecret" forKey:@"password" service:@"MyService"];

[UICKeyChainStore stringForKey:@"password" service:@"MyService"];

Specifying the service allows you to store keys for a particular service, maybe you have a Twitter or Facebook specific set of keys you wish to save.

As you can see we are not specifying the accessGroup. By default it will pick the first access-group specified in your Entitlements.plist when writing and will search across all access-groups when none is specified.

Allowing your second app to access your shared keychain data

So we have our first app which is saving data to the keychain group ($(AppIdentifierPrefix).com.my.firstapp) Now lets just say we decide to release a second app but we dont want users to have to re enter the same data, maybe we are persisting a login token or something so we want to try and read it if the user has our first app installed.

All you have to do to in order to be able to read from a shared keychain group is make sure both your apps use the same AppIdentifierPrefix and specify the keychain group of the keychain you want access to.

All apps that share the same AppIdentifierPrefix / Bundle Seed Id can access each others Keychains if permission has been granted in your Entitlements.plist file.

Setting up your project
  1. Add the Security.framework to your project
  2. Download UICKeyChainStore and add the files to your project
  3. Create a new Entitlements.plist file and specify the keychain group of the first app you want to be able to read data from.
    • We want access to the Keychain group $(AppIdentifierPrefix).com.my.firstapp for reading but we still want the default writing to occur in our own keychain group so we still specify $(AppIdentifierPrefix)$(CFBundleIdentifier) as the first / default group.
  4. Browse to your current app Targets Summary tab. Scroll down the bottom and you will see a section called Entitlements specify your Entitlements.plist file and make sure Enable Entitlements is checked.
Reading data from the shared Keychain

Try and read the value from the shared Keychain exactly how we did in our first app.

[UICKeyChainStore stringForKey:@"password" service:@"MyService"];

This should return the value supersecret.

Congratulations you just successfully shared some data between two different iOS apps via the Keychain!

For more details checkout:

Generic Keychain Example

Keychain Services Programming Guide

 

One Response to iOS Keychain: Sharing data between apps
  1. [...] overload? http://parislemon.com/post/23720620979/i-was-a-bit-surprised-when-i-first-saw-the-screen http:... ludwig-design.com/tech-news-tips-ios-and-windows-uiux

Leave a Reply

Your email address will not be published. Please enter your name, email and a comment.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>