RGLockbox 1.4.6

RGLockbox 1.4.6

TestsTested
LangLanguage Obj-CObjective C
License BSD
ReleasedLast Release Sep 2016
SPMSupports SPM

Maintained by Ryan Dignard, Ryan Dignard.



RGLockbox 1.4.6

  • 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.

The Swift version of this pod is named RGSwiftKeychain and is available on the branch swift-master.

Default supported types include:

  • NSData
  • NSString
  • NSDate
  • NSDictionary
  • NSArray
  • id<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.

Example

NSData* data = [@"abcd" dataWithEncoding:NSUTF8StringEncoding];
RGLockbox* lockbox = [RGLockbox manager];
[lockbox 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.

RGLockbox* lockbox = [RGLockbox manager];
NSData* data = [lockbox dataForKey:@"myData"];
NSString* string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
assert([string isEqual:@"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:

NSDate* date = [NSDate new];
RGLockbox* lockbox = [RGLockbox manager];
[lockbox setDate:date forKey:@"myDate"];
NSDate* readDate = [lockbox dateForKey:@"myDate"];
assert([readDate timeIntervalSince1970] == [date timeIntervalSince1970]);

NSString:

NSString* string = @"aString";
RGLockbox* lockbox = [RGLockbox manager];
[lockbox setString:string forKey:@"stringKey"];
NSString* readString = [lockbox stringForKey:@"stringKey"];
assert([readString isEqual:string]);

NSDictionary:

NSDictionary* dictionary = @{ @"aKey" : @"aValue" };
RGLockbox* lockbox = [RGLockbox manager];
[lockbox setJSONObject:dictionary forKey:@"dictionaryKey"];
NSDictionary* readDictionary = [lockbox JSONObjectForKey:@"dictionaryKey"];
assert([readDictionary isEqual:dictionary]);

NSArray:

NSArray* array = @[ @"aValue1", @"aValue2" ];
RGLockbox* lockbox = [RGLockbox manager];
[lockbox setJSONObject:array forKey:@"arrayKey"];
NSArray* readArray = [lockbox JSONObjectForKey:@"arrayKey"];
assert([readArray isEqual:array]);

id<NSCoding>:

NSURL* url = [NSURL URLWithString:@"google.com"];
RGLockbox* lockbox = [RGLockbox manager];
[lockbox setCodeable:url forKey:@"urlKey"];
NSURL* readURL = [lockbox codeableForKey:@"urlKey"];
assert([readURL isEqual:url]);

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.

NSDate* signupDate = [NSDate dateWithTimeIntervalSince1970:1453075980.0];
RGLockbox* lockbox = [[RGLockbox alloc] initWithNamespace:@"com.rglockbox.appbundle" accessibility:kSecAttrAccessibleAlways];
[lockbox setDate:signup forKey:@"userSignupDate"];
/* In another program, app extension, component framework, etc. ... */
RGLockbox* lockbox = [[RGLockbox alloc] initWithNamespace:@"com.rglockbox.appbundle" accessibility:kSecAttrAccessibleAlways];
NSDate* signupDate = [lockbox dateForKey:@"userSignupDate"];
assert([signupDate timeIntervalSince1970] == 1453075980.0);

Installation

Using cocoapods add pod 'RGLockbox' to your Podfile and run pod install

License

BSD Simplied (2-clause)