I am trying to write some data that is generated from a NSMutableArray like this
// set up keychain so I can write to it… or read if needed (specially for testing)
KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"KeychainTest" accessGroup:nil];
[keychain setObject:(__bridge id)(kSecAttrAccessibleWhenUnlocked) forKey:(__bridge id)(kSecAttrAccessible)];
//write to keychain
NSData *parsedRemoteSitesData = [NSKeyedArchiver archivedDataWithRootObject:parsedRemoteSitesMutableArray]; // converts MutableArray to NSData
[keychain setObject:parsedRemoteSitesData forKey:(__bridge id)(kSecValueData)]; pass data to keychain
my app is crashing on the last line there where I am trying to pass the NSData to the keychain, this is the error it is giving.
** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableData dataUsingEncoding:]: unrecognized selector sent to instance
I think this means that I have not encoded my NSData while passing the array to it but im not sure.. I was hoping someone could either tell me or provide some sample code to fix this.
any help would be greatly appreciated.
I have successfully stored a custom NSObject using:
NSData * data = [NSKeyedArchiver archivedDataWithRootObject: myObject];
KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"com.sth.sth" accessGroup:nil];
[keychainItem setObject:data forKey:kSecAttrAccount];
[keychainItem release];
and fetching using:
NSData * data;
KeychainItemWrapper *keychainItem = [[[KeychainItemWrapper alloc] initWithIdentifier:@"com.sth.sth" accessGroup:nil]autorelease];
data = [keychainItem objectForKey:kSecAttrAccount];
MyObject *obj = [NSKeyedUnarchiver unarchiveObjectWithData:data];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With