Storing NSArray or NSDictionary in keychainWrapper on IOS
keychainWrapper provides a mechanism for storing NSString
(like passwords).
Can I save NSArray
or NSDictionary
in keychainWrapper
?
source to share
You can use NSKeyedArchiver
and NSKeyedUnarchiver
, and if you have custom objects stored in a dictionary array, make sure the class implements the protocol NSCoding
.
NSData * encodedData = [NSKeyedArchiver archivedDataWithRootObject:array];
Then you can convert NSData to NSString using base64 encoding http://www.cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html
for unarchiving it just goes backwards, use get NSData
from base64 NSString
and use unarchiver
NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
source to share