I have an app that uses [NSUserDefaults standardUserDefaults] to store session information. Generally, this information is checked on app launch, and updated on app exit. I
I have the same problem with iOS 8 and the only solution worked for me is to delay perform of exit() function some duration (Ex.: 0.1 seconds) using:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 10), dispatch_get_main_queue(), ^{ exit(0); });
or create a method and then call it using performSelector:withObject:afterDelay:
- (void)exitApp {
exit(0);
}
[self performSelector:@selector(exitApp) withObject:nil afterDelay:0.1];
It looks like iOS 8 does not like setting strings in NSUserDefaults. Try encoding the string into NSData before saving.
When saving:
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:testString] forKey:@"Session"];
When reading:
NSData *_data = [[NSUserDefaults standardUserDefaults] objectForKey:@"Session"];
NSString *_dataArchive = [NSKeyedUnarchiver unarchiveObjectWithData:_data];
Hope this helps.
I found it in Foundation Framework Reference, think it will be useful:
The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData. For more details, see Preferences and Settings Programming Guide.
I faced the same issue. I solved it by calling
[[NSUserDefaults standardUserDefaults] synchronize];
before calling
[[NSUserDefaults standardUserDefaults] stringForKey:@"my_key"]
.
It turns out one has to call synchronize
not only after setting but before getting too.
It is bug in simulators.This bug also exist in prior to iOS8 beta4 on devices.But on devices this bug is resolved but it currently exist on simulators.They have also changed the simulator directories structure.If you reset your simulator it will work fine.On iOS8 devices it will also work fine.
Calling exit () in an iOS application is a criminal offence, and as you noticed, it got punished. You never quit an iOS application yourself. Never.