How to change the app preference settings through code? - iphone

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.

Related

Settings.bundle not respecting app group on iOS simulator or device

I had been using standardUserDefaults until recently but decided to use shared app groups because I created a watch extension. I have set up an app group and also added the ApplicationGroupContainerIdentifier to the settings bundle.
I have spent hours on this and, even after reading many articles, I just can’t get this to work. I am seeing that when I alloc my app group and print its contents, it has default values from standardUserDefaults.
All I am doing is [[NSUserDefaults alloc] initWithSuiteName: SUITE_NAME]] and then print its contents on the very next line.
I am puzzled by how it has values from my settings bundle. Moreover, when you edit the properties using the settings app, the settings in standardUserDefaults are changing but my app group values stay the same.
I see the same behaviour on the simulator and real device.
This turned out to be user error. There was a typo in the app group name as a result of the auto-correct feature. I did not realize it until I created a new app group.

App settings in iPhone Settings

I have been reading and exploring around adding settings for custom apps in the iPhone settings page.
And there is a question that is arising in my mind that, can we point an XIB / UIStoryboard-viewController which is designed by us into the root.plist.
This can help in displaying custom design and handling things differently as required.
For example, if custom app uses the passcode protection then we will need to save the passcode into the keychain and that will require additional logic to be written.
Cheers,
Kunjal
You cannot use custom UIViewControllers in the iOS settings page for your app. When using the Settings.bundle the preferences are stored in NSUserDefaults which does not integrate with the keychain. So you will need to include the settings view for the passcode in the app itself.

Are NSUserDefaults global to the entire device or just the app

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.

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.

Iphone - NSUserDefault settings do not appear on the settings page

I am an iOS development newbie. I am implementing some sample code, but try as I might I do not see my NSUserDefault settings on the settings page. What needs to be done for that? Also, where do you specify the UI for the settings page?
You need to define your settings bundle, see http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/Preferences/Preferences.html