TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | BSD |
ReleasedLast Release | Sep 2016 |
SPMSupports SPM | ✓ |
Maintained by Ryan Dignard.
RGLockbox
is a simple to use interface with the standard keychain. Using object-orientented approaches it is simple to pick a key and store any rudimentary value there.
There is a development Swift 3.0 version of this library available on the branch swift-3.
The Objective-C version of this pod is named RGLockbox
and is available on the branch objc-master.
Default supported types include:
NSData
String
NSDate
Dictionary
Array
NSCoding
NSURL
NSValue
(including NSNumber
and NSDecimalNumber
)NSNull
Note for safety Apple encourages developers to conform their objects to NSSecureCoding
instead of NSCoding
to prevent substitution attacks against your app.
IMPORTANT : when your application will terminate you should run dispatch_barrier_sync()
on keychainQueue
.
let data = "abcd".dataUsingEncoding(NSUTF8StringEncoding)
RGLockbox().setData(data, forKey: "myData")
Writing data is as simple as creating it and applying it to your keychain manager. By default these managers are namespaced to your bundle’s identifier.
let data = RGLockbox().dataForKey("myData")!
let string = String.init(data: data, encoding: NSUTF8StringEncoding)!
assert(string == "abcd")
Retrieving data is as simple as remembering your key assuming you use the same manager throughout. Mixing and matching managers with different namespaces is possible, but more of an advanced use case.
In addition to the primitive interface supporting reading and writing raw NSData
there is implicit support for a variety of types. 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)
Finally, this library supports arbitrary namespacing which allows sharing keychain data across app bundles as well as setting different item accessibility for advanced use cases.
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)
Using cocoapods add pod 'RGSwiftKeychain'
to your Podfile and run pod install
BSD Simplied (2-clause)