Coder Social home page Coder Social logo

swiftkeychain's Introduction

#SwiftKeychain Carthage compatible

Abstract

Swift wrapper for working with the Keychain API implemented with Protocol Oriented Programming.

You create an implementation of the KeychainGenericPasswordType protocol that encapsulates the data that you want to store in the Keychain. Most of the implementation is done for you by using default protocol implementations, such as setting the default service name and access mode (kSecAttrAccessibleWhenUnlocked).

Then you call the KeychainItemType methods to save, remove or fetch the item from the provided as argument KeychainServiceType protocol implementation.

SwiftKeychain Protocols

Let's say we want to store the access token and username for an Instagram account in the Keychain:

struct InstagramAccount: KeychainGenericPasswordType {
    
    let accountName: String
    let token: String
    var data = [String: AnyObject]()
    
    var dataToStore: [String: AnyObject] {
        return ["token": token]
    }
    
    var accessToken: String? {
        return data["token"] as? String
    }
    
    init(name: String, accessToken: String = "") {
        accountName = name
        token = accessToken
    }
}

In var dataToStore: [String: AnyObject] you return the Dictionary that you want to be saved in the Keychain and when you fetch the item from the Keychain its data will be populated in your var data: [String: AnyObject] property.

Save Item

let newAccount = InstagramAccount(name: "John", accessToken: "123456")

do {
    
    try newAccount.saveInKeychain()

} catch {
    
    print(error)
}

Note: The provided implementation of the KeychainServiceType protocol will replace the item if it already exists in the Keychain database.

Remove Item

let account = InstagramAccount(name: "John")

do {
    
    try account.removeFromKeychain()

} catch {
    
    print(error)
}

Fetch Item

var account = InstagramAccount(name: "John")

do {
    
    try account.fetchFromKeychain()
    
    if let token = account.accessToken {

        print("name: \(account.accountName), token: \(token)")
    }

} catch {

    print(error)
}

Installation

SwiftKeychain requires Swift 2.0 and Xcode 7 and supports iOS, OSX, watchOS and tvOS.

Manually

Copy the Keychain/Keychain.swift file to your project.

Carthage

Add the following line to your Cartfile

github "yankodimitrov/SwiftKeychain" "master"

CocoaPods

Add the following line to your Podfile

pod “SwiftKeychain”

License

SwiftKeychain is released under the MIT license. See the LICENSE.txt file for more info.

swiftkeychain's People

Contributors

p2 avatar yankodimitrov avatar ddengler avatar arielelkin avatar paulw11 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.