Are NSUserDefaults global to the entire device or just the app - iphone

I have a NSUserDefault which just stores whether the db has been configured or not. However after deleting the app and re-installing the nsuserdefault seems to remain?
NSString *dbIsConfigured = [[NSUserDefaults standardUserDefaults] stringForKey:#"dbIsConfigured"];

It is local for the app only. The other apps cannot be able to access that.
From Apple documentation for NSUserDefaults:
Sandbox Considerations
A sandboxed app cannot access or modify the preferences for any other app. (For example, if you add another app's domain using the addSuiteNamed: method, you do not gain access to that app's preferences.)
Attempting to access or modify with another app's preferences does not result in an error, but when you do, Mac OS X actually reads and writes files located within your app's container, rather than the actual preference files for the other application.

Related

NSUserDefaults and Update an application from app Store

In my app I'm using NSUserDefaults to store user data, when upgrading to a new version from App Store will those values be saved or deleted/ rebooted?
(for example: [prefs setInteger:_numberOfTimesAppOpened forKey:#"openApp"]; if before the upgrade the value is 70 after the upgrade will the value be 70/ 0/ or non existent?)
in case these parameters are deleted what is the right approach to save user data so it will be saved even when upgrading the application?
another question is if there is a way to store information on the device so if the user delete the app and then re-install it his previous data/ profile can be set?
Everything that is stored in UserDefaults will be still there after an update. They are persisting. User defaults will get deleted when the app is removed from the device by the user.
And there is no way to store if an app was installed already once. At least not on the device.

App Settings change: 2 synchronizes for StandardUserDefaults necessary?

I am changing app settings within my iPhone application.
Strangely, I have to do 2 synchronize commands with StandardUserDefaults to make my changes to be reflected in the app settings.
Secondly, when I change my "preferred user language" within my iPhone application, I do have to start my app twice for the language change.
What's the reason for that?
Is there a way to dump the StandardUserDefaults and to see all the settings stored there?
Thanks for your help!
Is there a way to dump the StandardUserDefaults and to see all the settings stored there?
Yes, that is pretty easy to do.
The NSUserDefaults are stored in a plist file located in your app's sandboxed environment inside the Library folder.
For checking this on the simulator, have a look at
~/Library/Application Support/iPhone
Simulator/5.0/Applications/[hashed app
identifier]/Library/Preferences/[application bundle identifier].plist
Note that you will have to replace the values in brackets to find your specific app. Also note that this path works for the iOS5.0 environment - for others, you will have to replace the 5.0 with whatever system version you are working with.

Are values stored in NSUserDefaults removed when the app that put them there is uninstalled?

If I put a token (a string) into NSUserDefaults, lets say as a paramter passed to a REST API that is used by the app, and the app is uninstalled, will the string remain on the device?
No, it will not. I use NSUserDefaults in the exact same manner, and it will not stay after the app is deleted. You can verify this via Organizer if you need to.
It will however persist through updates. I have been using TestFlightApp for all of my beta testing, and the token (and other saved user default data) remains. Hope this helps.
No. You can see that the data saved is in Library/Preferences/ inside your sandbox. If you are using Simulator, see (something like) ~/Library/Application\ Support/iPhone\ Simulator/4.3.2/Applications/00DB5581-E797-4AB0-9033-321ACD8938BD/Library/Preferences/com.me.MyApp.plist

saving a file outside of my application bundel in iphone

i want to save a file outside of my application bundle as a backup of database so that when user reinstall my app or my app crashes he can backup his data from that location.
Apple already does this, with iTunes.
What you are asking is technically against apple's policy, unless you store it off site, on a server (which you could easily do)
If you have a lot of applications, I'm not aware of the policy, but perhaps you could make an app to back up your other app's data.

How to change the app preference settings through code?

I have listed my app in the device setting app.Now I can change my app settings through the device app settings app.
Now what I am trying to do is the reverse.
I am trying to make a app so that it can itself change its preferences in the device settings app if I change the preferences of my app from within the app.
Any ideas Friends,
Thank You All.
Nice and simple:
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"MyPreferenceKey"];
NSUserDefaults includes convenience methods for setting ints, floats, strings, and so on. You can also just use -setValue:forKey:.
When you change a value in your app using NSUserDefaults, the setting well change in the Settings app, too.