I've got a user object that I need to store in NSUserDefaults and share with an iOS 8 extension app (Watchkit). In the main container app, I can encode and decode the object without any problem. However, when I try to retrieve the stored user object in the Extension, I get a "'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class"
error.
As far as I can see NSCoding has been implemented correctly in the object (and I'm able to encode and decode the object in the 'main' app).
Code in 'Container' app to store user object.
//Store user data in NSUserDefaults
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.mygroup"];
SFUserAccount *user = [SFUserAccountManager sharedInstance].currentUser;
NSData *userEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:user];
[defaults removeObjectForKey:@"SF User Acct1"]; //remove any old values
[defaults setObject:userEncodedObject forKey:@"SF User Acct1"];
[defaults synchronize];
SFUserAccount *decodedUser = [NSKeyedUnarchiver unarchiveObjectWithData:userEncodedObject];
The last line above to perform a test decode in the main app works fine.
The code to retrieve from NSUserDefaults and decode in the extension target is below.
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.mygroup"];
NSData *archivedUser = [defaults objectForKey:@"SF User Acct1"];
if (archivedUser){
SFUserAccount *user = [NSKeyedUnarchiver unarchiveObjectWithData:archivedUser];
}
In the extension code, I get a "'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class"
Any suggestions as to where I should start looking? The app compiles fine which leads me to believe that the required frameworks have been included in the Extension target.
For your WatchKit Extension to be able to decode SFUserAccount objects, it needs to understand the SFUserAccount class. To enable this, it should be added to your WatchKit Extension.
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