Locksmith
A sane way to work with the iOS Keychain in Swift.
Installation
CocoaPods
Locksmith is available through CocoaPods. To install Locksmith for Swift 2, simply add the following line to your Podfile:
pod 'Locksmith', :git => 'https://github.com/matthewpalmer/Locksmith.git', :branch => '2.0'
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
try Locksmith.saveData(["some key": "some value"], forUserAccount: "myUserAccount", inService: "myService")
Load data
let dictionary = Locksmith.loadDataForUserAccount("myUserAccount", inService: "myService")
Update data
try Locksmith.updateData(["some key": "another value"], forUserAccount: "myUserAccount", inService: "myService")
Delete data
try 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
try Locksmith.performRequest(saveRequest)
Reading
let readRequest = LocksmithRequest(userAccount: userAccount, service: service)
let dictionary = try Locksmith.performRequest(readRequest)
Deleting
let deleteRequest = LocksmithRequest(userAccount: userAccount, requestType: .Delete, service: service)
try 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.