NSUserDefaults boolForKey not working in iOS 8
Ok, after playing with this some more, I found the answer to my question here:
NSUserDefaults not cleared after app uninstall on simulator
As mentioned above, the problem is that the simulator seems to store NSUserDefaults even after deleting the app. That's why Rob could not reproduce the issue, it works fine the first time you try but if you delete the app and try again it doesn't work.
Will there be any conflict with other applications when my application saves data in [NSUserDefaults standardUserDefaults]?
No, each application has it's own defaults, saved in a different file based on the application's bundle ID. On the iPhone (and simulator) each app has it's own home directory too, and can't access other app's files.
iOS: Is it secure to store sensible user information in [NSUserDefaults standardUserDefaults]?
Other apps would not be able to access information saved in your user defaults for your app. But this doesn't mean the information can't be obtained. You can plug your device into Xcode and run your app. Under devices, you can view your own app's information, and the information saved in user defaults will be listed there.
It generally isn't a good idea to save sensitive user data there, while not that easy to access, it is still accessible. For general non-personal data or settings, it really isn't a big issue. You can always use the built-in keychain access to store username and password information and use user defaults for anything else you might need.
In one of my apps, I salt and hash the username and password together to create a unique token. I save that in user defaults. It is worthless by itself, but that is just my way of doing things. Good luck.
iOS 8.2 [NSUserDefaults standardUserDefaults] returning nil
Did you set the dictionary to use as "Settings.bundle/Root.plist"
?
// Register the preference defaults from file "Settings.bundle/Root.plist"
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@"Settings.bundle/Root"
ofType:@"plist"]];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
Thereafter [NSUserDefaults standardUserDefaults]
no longer is nil.
In my case, the dictionary used by [NSUserDefaults standardUserDefaults]
looks like this in the debugger:
{
PreferenceSpecifiers = (
{
DefaultValue = 1;
Key = sortByDistance;
Title = "Sortiere nach Entfernung";
Type = PSToggleSwitchSpecifier;
}
);
StringsTable = Root;
}
To access the preferences I've written a tiny method:
- (id) preferenceValueForKey: (NSString *)key {
NSArray *preferences = [[NSUserDefaults standardUserDefaults] arrayForKey:@"PreferenceSpecifiers"];
NSUInteger index = [preferences indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return [[obj valueForKey:@"Key"] isEqualToString:key];
}];
return [preferences[index] valueForKey:@"DefaultValue"];
}
Related Topics
How to Lock Portrait Orientation for Only Main View Using Swift
Change Color of Png in Buttons - iOS
Refresh Certain Row of Uitableview Based on Int in Swift
Using Convertpoint to Get the Relative Position Inside a Parent Uiview
Logging Method Signature Using Swift
Align Text Using Drawinrect:Withattributes:
How to Use Autocorrection and Shortcut List in iOS8 Custom Keyboard
iOS Avplayer Trigger Streaming Is Out of Buffer
What Happens with Constraints When a View Is Removed
Swift - Segmented Control - Switch Multiple Views
Popping and Pushing View Controllers in Same Action
Wait for Async Task to Finish Completion Block Before Returning in App Delegate
Afnetworking 2.0 Track File Upload Progress
Where Are iOS Simulator Screenshots Stored