A sane way to work with the iOS Keychain in Swift.
Install the framework (reference c/o Alamofire)
git submodule add https://github.com/matthewpalmer/Locksmith.git
- Open the newly created folder, 'Locksmith', in Finder
- Drag Locksmith.xcodeproj to the file navigator (left sidebar) of your project
- Click on your app's target, then click on Build Phases
- Follow the gif
import Locksmith
wherever you need it
Note: Due to a bug in Swift, the Swift Compiler - Code Generation
Optimization Level for release builds has to be set to -Onone. Go here for more infromation on how to change it.
Save Data
Locksmith.saveData(["some key": "some value"], inService: "myService", forUserAccount: "myUserAccount")
Load Data
let (dictionary, error) = Locksmith.loadData(inService: "myService", forUserAccount: "myUserAccount")
Update Data
Locksmith.updateData(["some key": "another value"], inService: "myService", forUserAccount: "myUserAccount")
Delete Data
Locksmith.deleteData(inService: "myService", forUserAccount: "myUserAccount")
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
let saveRequest = LocksmithRequest(service: service, userAccount: userAccount, data: ["some key": "some value"])
// Customize the request
saveRequest.synchronizable = true
Locksmith.performRequest(saveRequest)
Reading
let readRequest = LocksmithRequest(service: service, userAccount: userAccount)
let (dictionary, error) = Locksmith.performRequest(readRequest)
Deleting
let deleteRequest = LocksmithRequest(service: service, userAccount: userAccount, requestType: .Delete)
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 .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