KeyClip 1.4.0

KeyClip 1.4.0

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布最后发布2016年11月
SwiftSwift 版本3.0.1
SPM支持 SPM

Shinichiro Aska 维护。



KeyClip 1.4.0

  • 作者:
  • aska

Build Status

KeyClip 是用 Swift 编写的另一个 Keychain 库。

功能

使用方法

String

KeyClip.save("access_token", string: "********") // -> Bool

let token = KeyClip.load("access_token") as String?

KeyClip.delete("access_token") // Remove the data

KeyClip.clear() // Remove all the data

KeyClip.exists("access_token") // -> Bool

NSDictionary

必须兼容 NSJSONSerialization。

有效的 JSON 元素是 Dictionary,Array,String,Number,Boolean 和 null。

KeyClip.save("account", dictionary: ["name": "aska", "token": "******"]) // -> Bool

let dictionary = KeyClip.load("account") as NSDictionary?

NSData

KeyClip.save("data", data: NSData()) // -> Bool

let data = KeyClip.load("data") as NSData?

你的类

KeyClip.save("account", dictionary: account.dictionaryValue)

let account = KeyClip.load("account") { (dictionary) -> Account in
    return Account(dictionary)
}

class Account {
    let name: String
    let password: String

    init(_ dictionary: NSDictionary) {
        self.name = dictionary["name"] as String
        self.password = dictionary["password"] as String
    }

    var dictionaryValue: [String: String] {
        return ["name": name, "password": password]
    }
}

错误处理

返回值

let success = KeyClip.save("password", string: "********")
if !success {
    // Show Alert "Saving password to keychain failed"
}

Clojure

KeyClip.save("password", string: "********") { error in
    let status = error.code // OSStatus
    // Show Alert "Saving failed \(error.localizedDescription)(\(error.code))"
}

调试打印

KeyClip.printError(true)

设置

let clip = KeyClip.Builder()

    // kSecAttrService
    .service(NSBundle.mainBundle().bundleIdentifier) // default

    // kSecAttrAccessible
    .accessible(kSecAttrAccessibleAfterFirstUnlock) // default

    // kSecAttrAccessGroup
    .accessGroup("XXXX23F3DC53.com.example.share") // default is nil

    .build()

关于 accessGroup 的备注

⚠️iOS 模拟器的 keychain 实现不支持 kSecAttrAccessGroup。(始终为 "test")

⚠️kSecAttrAccessGroup 必须与 App Identifier 前缀匹配。https://developer.apple.com/library/mac/documentation/Security/Reference/keychainservices/index.html

如何检查 App Identifier

Entitlement.plist 的 keychain-access-groups 或 App Identifier。

KeyClip.defaultAccessGroup() // -> String (eg. XXXX23F3DC53.*)

要求

  • iOS 8.0+ / Mac OS X 10.10+
  • Xcode 8

许可证

KeyClip 采用 MIT 许可证发布。有关详细信息,请参阅 LICENSE。