Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an error trying to write NSData to Keychain

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.

like image 596
HurkNburkS Avatar asked Feb 15 '23 21:02

HurkNburkS


1 Answers

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];
like image 104
Yunus Nedim Mehel Avatar answered Mar 03 '23 03:03

Yunus Nedim Mehel