A sane way to work with the iOS Keychain in Swift.
Locksmith is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "Locksmith"
Note: Swift pods are currently only available in beta, so you need to gem install cocoapods --pre to use Locksmith.
Alternatively, you can simply drag the two files Locksmith.swift and LocksmithRequest.swift into your project.
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")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)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 .ReadOptional
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 falseMatthew Palmer, matt@matthewpalmer.net
Locksmith is available under the MIT license. See the LICENSE file for more info.