2014-03-10

In iOS Keychain Tutorial, I have explained how to store data securely in Objective-C and how to share secured data between apps.

What is Keychain ?

Keychain is an encrypted container where you can store secured information like passwords,certificates,identities,…etc. In iOS, each Application has it’s own keychain. To share the data between apps, they should have the same Access Group in code signing entitlements.


Most useful keychain API:
1. SecItemAdd – used to add an item to keychain
2. SecItemUpdate – used to modify an existing keychain item.
3. SecItemCopyMatching – used find an item in keychain and get information.
Note: To user keychain in iOS, you need to add Security.Framework

For all security API queries, we need to pass a dictionary like below:

kSecClass -> type of item which is kSecClassGenericPassword
kSecAttrGeneric -> Generic attribute key.
kSecAttrAccount -> Account attribute key
kSecAttrService -> Name of the service.
kSecAttrAccessible -> Specifies when data is accessible.
kSecAttrAccessGroup : Keychain access group name. This should be same to share the data across apps.

How to enable sharing data across apps ?

In your project’s Target Build Settings, enable Keychain Sharing and group name as shown in the below image.



 

This will create entitlements file in the project as shown in the below image.



Content of entitlements file is:

Check this link for getting AppIdentifierPrefix programatically.

I have created a wrapper class named Keychain to access the keychain data Source code of: Keychain.h

Source code of: Keychain.m

Add the Keychain class to your project to access keychain data

1). Initialization of the class

First define your service name and group name.

 

2). How to Add an item to keychain

 

3). Finding an item in the keychain

 

4) .Update an item in the keychain

 

5). Remove an item from keychain

Reference: Apple Documentation

The post iOS KeyChain Tutorial appeared first on hayaGeek.

Show more