RGSwiftKeychain 2.2.8

RGSwiftKeychain 2.2.8

测试已测试
语言语言 SwiftSwift
许可 BSD
发布最后发布2016 年 9 月
SPM支持 SPM

Ryan Dignard 维护。



  • Ryan Dignard

Readme Score

RGSwiftKeychain

RGLockbox 是一个简单易用的标准密钥链接口。使用面向对象的方案,您可以选择一个密钥并在其中存储任何基本值。

该库在分支 swift-3 提供了 Swift 3.0 的发展版本。

此 pod 的 Objective-C 版本名为 RGLockbox,可在分支 objc-master 上找到。

默认支持的类型包括

  • NSData
  • String
  • NSDate
  • Dictionary
  • Array
  • NSCoding
    • NSURL
    • NSValue(包括 NSNumberNSDecimalNumber
    • NSNull

安全提示:为了防止对您的应用进行替换攻击,Apple 鼓励开发者将对象符合 NSSecureCoding 而不是 NSCoding

重要:当您的应用程序将要终止时,您应该在 keychainQueue 上运行 dispatch_barrier_sync()

例子

let data = "abcd".dataUsingEncoding(NSUTF8StringEncoding)
RGLockbox().setData(data, forKey: "myData")

写入数据就像创建它并将其应用到您的密钥链管理器一样简单。默认情况下,这些管理器都以其捆绑标识符命名空间。

let data = RGLockbox().dataForKey("myData")!
let string = String.init(data: data, encoding: NSUTF8StringEncoding)!
assert(string == "abcd")

检索数据就像记住您的密钥一样简单,前提是您在整个过程中使用相同的管理器。混合和匹配具有不同命名空间的管理器是可能的,但更适用于高级用例。

除了支持读取和写入原始 NSData 的原始接口之外,还隐式支持各种类型。例如,NSDate

let date = NSDate.init()
RGLockbox().setDate(date, forKey: "myDate")
let readDate = RGLockbox().dateForKey("myDate")!
assert(Int(date.timeIntervalSince1970) == Int(readDate.timeIntervalSince1970))

String:

let string = "aString"
RGLockbox().setString(string, forKey: "stringKey")
let readString = RGLockbox().stringForKey("stringKey")!
assert(string == readString)

Dictionary:

let dictionary = [ "aKey" : "aValue" ]
RGLockbox().setJSONObject(dictionary, forKey: "dictionaryKey")
let readDictionary = RGLockbox().JSONObjectForKey("dictionaryKey")!
assert(dictionary == readDictionary)

Array:

let array = [ "aValue1", "aValue2" ]
RGLockbox().setJSONObject(array, forKey: "arrayKey")
let readArray = RGLockbox().JSONObjectForKey("arrayKey")!
assert(array == readArray)

NSCoding:

let url = NSURL.init(string: "google.com")
RGLockbox().setCodeable(url, forKey: "urlKey")
let readURL = RGLockbox().codeableForKey("urlKey")!
assert(url == readURL)

最后,该库支持任意命名空间,允许在应用程序捆绑包之间共享密钥链数据,以及为高级用例设置不同的项目访问权限。

let signupDate = NSDate.init(timeIntervalSince1970: 1453075980.0)
let lockbox = RGLockbox.init(withNamespace: "com.rglockbox.appbundle", accessibility: kSecAttrAccessibleAlways, accessGroup: "com.rglockbox")
lockbox.setDate(signupDate, forKey: "userSignupDate")

/* In another program, app extension, component framework, etc. ... */

let lockbox = RGLockbox.init(withNamespace: "com.rglockbox.appbundle", accessibility: kSecAttrAccessibleAlways, accessGroup: "com.rglockbox")
let signupDate = lockbox.dateForKey("userSignupDate")!
assert(signupDate.timeIntervalSince1970 == 1453075980.0)

安装

使用 Cocoapods,将 pod 'RGSwiftKeychain' 添加到您的 Podfile 中,然后运行 pod install

许可

BSD 简化版(2 条款)