KeychainAccess
KeychainAccess is a simple Swift wrapper for Keychain that works on iOS and OS X. Makes using Keychain APIs extremely easy and much more palatable to use in Swift.
KeychainAccess is a simple Swift wrapper for Keychain that works on iOS and OS X. Makes using Keychain APIs extremely easy and much more palatable to use in Swift.
Features
Simple interface
Support access group
Support accessibility
Support iCloud sharing
Support TouchID and Keychain integration (iOS 8+)
Support Shared Web Credentials (iOS 8+)
Works on both iOS & OS X
watchOS and tvOS are supported
Swift 4 & Swift 3 compatible
:book: Usage
:eyes: See also:
:key: Basics
Saving Application Password
Saving Internet Password
:key: Instantiation
Create Keychain for Application Password
Create Keychain for Internet Password
:key: Adding an item
subscripting
for String
for NSData
set method
error handling
:key: Obtaining an item
subscripting
for String (If the value is NSData, attempt to convert to String)
for NSData
get methods
as String
as NSData
:key: Removing an item
subscripting
remove method
:key: Set Label and Comment
:key: Obtaining Other Attributes
PersistentRef
Creation Date
All Attributes
subscripting
:key: Configuration (Accessibility, Sharing, iCloud Sync)
Provides fluent interfaces
Accessibility
Default accessibility matches background application (=kSecAttrAccessibleAfterFirstUnlock)
For background application
Creating instance
One-shot
For foreground application
Creating instance
One-shot
:couple: Sharing Keychain items
:arrows_counterclockwise: Synchronizing Keychain items with iCloud
Creating instance
One-shot
:fu: Touch ID integration
Any Operation that require authentication must be run in the background thread.
If you run in the main thread, UI thread will lock for the system to try to display the authentication dialog.
:closed_lock_with_key: Adding a Touch ID protected item
If you want to store the Touch ID protected Keychain item, specify accessibility
and authenticationPolicy
attributes.
:closed_lock_with_key: Updating a Touch ID protected item
The same way as when adding.
Do not run in the main thread if there is a possibility that the item you are trying to add already exists, and protected.
Because updating protected items requires authentication.
Additionally, you want to show custom authentication prompt message when updating, specify an authenticationPrompt
attribute.
If the item not protected, the authenticationPrompt
parameter just be ignored.
:closed_lock_with_key: Obtaining a Touch ID protected item
The same way as when you get a normal item. It will be displayed automatically Touch ID or passcode authentication If the item you try to get is protected.
If you want to show custom authentication prompt message, specify an authenticationPrompt
attribute.
If the item not protected, the authenticationPrompt
parameter just be ignored.
:closed_lock_with_key: Removing a Touch ID protected item
The same way as when you remove a normal item.
There is no way to show Touch ID or passcode authentication when removing Keychain items.
:key: Shared Web Credentials
Shared web credentials is a programming interface that enables native iOS apps to share credentials with their website counterparts. For example, a user may log in to a website in Safari, entering a user name and password, and save those credentials using the iCloud Keychain. Later, the user may run a native app from the same developer, and instead of the app requiring the user to reenter a user name and password, shared web credentials gives it access to the credentials that were entered earlier in Safari. The user can also create new accounts, update passwords, or delete her account from within the app. These changes are then saved and used by Safari.
https://developer.apple.com/library/ios/documentation/Security/Reference/SharedWebCredentialsRef/
Request all associated domain's credentials
Generate strong random password
Generate strong random password that is in the same format used by Safari autofill (xxx-xxx-xxx-xxx).
How to set up Shared Web Credentials
- Add a com.apple.developer.associated-domains entitlement to your app. This entitlement must include all the domains with which you want to share credentials.
- Add an apple-app-site-association file to your website. This file must include application identifiers for all the apps with which the site wants to share credentials, and it must be properly signed.
- When the app is installed, the system downloads and verifies the site association file for each of its associated domains. If the verification is successful, the app is associated with the domain.
More details:
https://developer.apple.com/library/ios/documentation/Security/Reference/SharedWebCredentialsRef/
:key: Debugging
Display all stored items if print keychain object
=>
[
[authenticationType: default, key: kishikawakatsumi, server: github.com, class: internetPassword, protocol: https]
[authenticationType: default, key: hirohamada, server: github.com, class: internetPassword, protocol: https]
[authenticationType: default, key: honeylemon, server: github.com, class: internetPassword, protocol: https]
]
Obtaining all stored keys
=>
key: kishikawakatsumi
key: hirohamada
key: honeylemon
Obtaining all stored items
=>
item: [authenticationType: Default, key: kishikawakatsumi, server: github.com, class: InternetPassword, protocol: https]
item: [authenticationType: Default, key: hirohamada, server: github.com, class: InternetPassword, protocol: https]
item: [authenticationType: Default, key: honeylemon, server: github.com, class: InternetPassword, protocol: https]
Requirements
OS | Swift | |
---|---|---|
v1.1.x | iOS 7+, OSX 10.9+ | 1.1 |
v1.2.x | iOS 7+, OSX 10.9+ | 1.2 |
v2.0.x | iOS 7+, OSX 10.9+, watchOS 2+ | 2.0 |
v2.1.x | iOS 7+, OSX 10.9+, watchOS 2+ | 2.0 |
v2.2.x | iOS 8+, OSX 10.9+, watchOS 2+, tvOS 9+ | 2.0, 2.1 |
v2.3.x | iOS 8+, OSX 10.9+, watchOS 2+, tvOS 9+ | 2.0, 2.1, 2.2 |
v2.4.x | iOS 8+, OSX 10.9+, watchOS 2+, tvOS 9+ | 2.2, 2.3 |
v3.0.x | iOS 8+, OSX 10.9+, watchOS 2+, tvOS 9+ | 3.x |
v3.1.x | iOS 8+, OSX 10.9+, watchOS 2+, tvOS 9+ | 4.0, 4.1 |
Installation
CocoaPods
KeychainAccess is available through CocoaPods. To install
it, simply add the following lines to your Podfile:
Carthage
KeychainAccess is available through Carthage. To install
it, simply add the following line to your Cartfile:
github "kishikawakatsumi/KeychainAccess"
Swift Package Manager
KeychainAccess is also available through Swift Package Manager.
First, create Package.swift
that its package declaration includes:
Then, type
To manually add to your project
- Add
Lib/KeychainAccess.xcodeproj
to your project - Link
KeychainAccess.framework
with your target - Add
Copy Files Build Phase
to include the framework to your application bundle
See iOS Example Project as reference.
