Code Monkey home page Code Monkey logo

locksmith's Introduction

Locksmith

A sane way to work with the iOS Keychain in Swift.

Version Carthage compatible License Platform

Installation

CocoaPods

Locksmith is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "Locksmith"

Swift 2 support is available via the 2.0 branch.

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, [email protected]

License

Locksmith is available under the MIT license. See the LICENSE file for more info.

locksmith's People

Contributors

matthewpalmer avatar larslockefeer avatar mwildehahn avatar marcelofabri avatar chaoscoder avatar jonallured avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.