This is Locksmith’s compatibility branch for Swift 1.2
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", :git => 'https://github.com/matthewpalmer/Locksmith.git', :branch => '1.2.2'
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
- writes the data to the keychain if it does not exist already
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
- overwrites whatever is stored on the keychain under this user account (if nothing is stored, we save as normal)
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.