Locksmith
A sane way to work with the iOS Keychain in Swift.
Installation
CocoaPods
Locksmith is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "Locksmith"
Manual
Alternatively, you can simply drag the two files Locksmith.swift
and LocksmithRequest.swift
into your project.
Quick Start
In the following examples, you can choose not to provide a value for the inService
parameter, and it will default to your Bundle Identifier.
Save data
let error = Locksmith.saveData(["some key": "some value"], forUserAccount: "myUserAccount")
Save data, specifying a service
let error = Locksmith.saveData(["some key": "some value"], forUserAccount: "myUserAccount", inService: "myService")
Load data
let (dictionary, error) = Locksmith.loadDataForUserAccount("myUserAccount")
Load data, specifying a service
let (dictionary, error) = Locksmith.loadDataForUserAccount("myUserAccount", inService: "myService")
Update data
let error = Locksmith.updateData(["some key": "another value"], forUserAccount: "myUserAccount")
Update data, specifying a service
let error = Locksmith.updateData(["some key": "another value"], forUserAccount: "myUserAccount", inService: "myService")
Delete data
let error = Locksmith.deleteDataForUserAccount("myUserAccount")
Delete data, specifying a service
let error = Locksmith.deleteDataForUserAccount("myUserAccount", inService: "myService")
Custom Requests
To create custom keychain requests, you first have to instantiate a LocksmithRequest
. This request can be customised as much as required. Then callLocksmith.performRequest
on that request.
Saving
// As above, the `service` parameter will default to your Bundle Identifier if omitted.
let saveRequest = LocksmithRequest(userAccount: userAccount, data: ["some key": "some value"], service: service)
// Customize the request
saveRequest.synchronizable = true
Locksmith.performRequest(saveRequest)
Reading
let readRequest = LocksmithRequest(userAccount: userAccount, service: service)
let (dictionary, error) = Locksmith.performRequest(readRequest)
Deleting
let deleteRequest = LocksmithRequest(userAccount: userAccount, requestType: .Delete, service: service)
Locksmith.performRequest(deleteRequest)
LocksmithRequest
Use these attributes to customize your LocksmithRequest
instance.
If you need any more custom attributes, either create a pull request or open an issue.
Required
var service: String
var userAccount: String
var type: RequestType // Defaults to .Read
Optional
var group: String? // Used for keychain sharing
var data: NSDictionary? // Used only for write requests
var matchLimit: MatchLimit // Defaults to .One
var securityClass: SecurityClass // Defaults to .GenericPassword
var synchronizable: Bool // Defaults to false
Testing
I can't work out why, but opening Example/Locksmith.xcworkspace
and trying to run the tests from there won't work. (Pull requests greatly appreciated on this!) Instead, you can run the tests by opening Locksmith.xcodeproj
in the root directory, and doing Product -> Test.
Author
Matthew Palmer, matt@matthewpalmer.net
License
Locksmith is available under the MIT license. See the LICENSE file for more info.