I am doing an iphone game and saving data using NSUserDefaults. Here is how I save a user:
-(void) saveUser:(User*)user forPosition:(int) position{
NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:user];
NSUserDefaults*savedData=[NSUserDefaults standardUserDefaults];
[savedData setValue:myEncodedObject forKey:[NSString stringWithFormat:@"user%i",position]];
[savedData synchronize];
self.currentUser=user;}
And here is how I load it:
-(BOOL) loadUser:(int) position{
if([savedData objectForKey:[NSString stringWithFormat:@"user%i",position]]){
NSData *encodedObject = [savedData objectForKey:[NSString stringWithFormat:@"user%i",position]];
User *user = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
self.currentUser=user;
return YES;
}
else return NO;}
As you can see, I am saving a custom class on NSUserDefaults. To do that I used guidance from How to store custom objects in NSUserDefaults (in case anyone is interested).
When I use this in the simulator it works perfectly. The problem is that if I run this on my iphone (iphone 6 on ios 8.1) it doesn't load anything.
Any ideas of what it could be? I've already visited this question NSUserdefaults works on Simulator but not on Device but nothing worked. Thanks!