Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an update NSUserDefault on iOS 4?

I am developing an iPhone application, I encounter a problem on the iOS4 because of multi task.

This application has the default settings defined in a Settings.bundle. If I run my application then I left it (so it goes in the background). I'll change the settings and restarts the application (it comes out of standby and method: applicationDidBecomeActive () is called).

Values in NSUserDefault are not updates, but when I leave the application and relaunch. The values are good.

Does someone experience the same problem as me? Is what I'm doing something wrong?

Thank you for your advice / help.

like image 977
Mathieu Mahé Avatar asked Jul 07 '10 13:07

Mathieu Mahé


People also ask

What is Nsuserdefault?

The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an app to customize its behavior to match a user's preferences. For example, you can allow users to specify their preferred units of measurement or media playback speed.

Where are NSUserDefaults stored?

All the contents saved by NSUserDefaults is saved inside a plist file that can be found under Library -> Preferences -> $AppBundleId.

Is NSUserDefaults secure?

Because NSUserDefaults stores all data in an unencrypted . plist file, a curious person could potentially view this data with minimal effort. That means that you should never store any type of sensitive data inside NSUserDefaults.

Is NSUserDefaults thread safe?

The NSUserDefaults class is thread-safe.

What is nsuserdefaults class in iOS?

The NSUserDefaults class provides a way for iOS Apps and Extensions to programmatically interact with the system-wide Defaults System. By using the Defaults System, the user can configure an app's behavior or styling to meet their preferences (based on the design of the app).

What is the use of nsuserdefaults?

With the NSUserDefaults class, you can save settings and properties related to application or user data. For example, you could save a profile image set by the user or a default color scheme for the application. The objects will be saved in what is known as the iOS “defaults system”.

How do I update my iPhone to the latest iOS?

When an update is available, iPhone downloads and installs the update overnight while charging and connected to Wi-Fi. You’re notified before an update is installed. At any time, you can check for and install software updates. Go to Settings > General > Software Update.

What happens to my data and settings when I update iOS?

When you update to the latest version of iOS, your data and settings remain unchanged. See More...


2 Answers

I had the same problem as you, and got around it by calling

[[NSUserDefaults standardUserDefaults] synchronize];

in applicationDidBecomeActive.

For some reason the [NSUserDefaults standardUserDefaults] object you can access in your app isn't synchronized with the actual plist files backing it when an app becomes active again after having been suspended. Calling the synchronize method forces a synchronization.

like image 146
Douwe Maan Avatar answered Sep 25 '22 10:09

Douwe Maan


You need to listen for settings changes. Best way to do it is to set up a listener wherever you need it:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(settingsChanged) name:NSUserDefaultsDidChangeNotification object:nil];

The only thing you need to make sure of is that you don't change any settings in your listener, or you get infinite recursion without a little more logic being added in.

I'd stay away from synchronizing because in my experience I found I had to run it twice. But that's accidental behavior. A listener will notify you when the new settings have been re-read from the store.

like image 44
ZaBlanc Avatar answered Sep 26 '22 10:09

ZaBlanc