Getting error while trying to write NSData to Keychain

I am trying to write some data that is generated from 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 application is broken to the last line where I try to pass NSData to the keychain, this is the error it gives.

 ** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableData dataUsingEncoding:]: unrecognized selector sent to instance 

I think this means that I did not encode my NSData when passing the array, but I'm not sure. I was hoping someone could tell me or provide sample code to fix this.

Any help would be greatly appreciated.

+4
source share
1 answer

I successfully saved 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 sampling using:

 NSData * data; KeychainItemWrapper *keychainItem = [[[KeychainItemWrapper alloc] initWithIdentifier:@"com.sth.sth" accessGroup:nil]autorelease]; data = [keychainItem objectForKey:kSecAttrAccount]; MyObject *obj = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 
+8
source

Source: https://habr.com/ru/post/1491742/


All Articles