Why is this simple settings bundle not reading anything? - iphone

I have a settings bundle with the following settings. When I build/install my app the Settings.app appears to work correctly. I see the default value and everything. But in my app when trying to read the text field's text I get nil.
Here is how I read the settings
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *temp = [defaults stringForKey:#"url_preference"];

The defaults in the settings bundle are only read by the Settings app. See this question for details and workarounds.

Related

Change application settings through settings bundle while application is running IOS

I have written code in -(void)applicationWillEnterForeground:(UIApplication *)application , but my application is not detecting the change made in the settings bundle, new settings work when application restarts, someone please help me how to make changes in application's settings while application is running
Try to -(BOOL)synchronize; the NSUserDefaults.
After setting the values in NSUserDefaults try calling [[NSUserDefaults standardUserDefaults] synchronize]; so that the values get written to disk.
in your applicationWillEnterForeground: methode after this line
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults], add this line
[defaults synchronize];

How to read settings from NSUserDefaults when using inappsettingskit

I am new to XCode and iOS programming. So please assume nothing ...
I am trying to incorporate the inappsettings bundle into my project. It is the 3rd party product found at http://inappsettingskit.com/
I have included all (I think) the files from the kit as well as their settings.app bundle. Through IB, I have built a tab bar where each tab item is a navigational controller. The view of my "Settings" tab is a view controller of class IASKAppSettingsViewController.
When I run my app, the default setting screen included in the kit displays and everything seems to run correctly.
However, I cannot figure out how to actually use the settings that I select in my app. I believe that the settings are stored in [NSUserDefaults standardUserDefaults], but when I query this as below, NSString* s always comes back as nil.
NSUserDefaults* d = [NSUserDefaults standardUserDefaults];
NSString* s = [d objectForKey:#"ROOKIE"];
I have also tried initializing [[NSUserDefaults standardUserDefaults] as follows:
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Root~iphone.InApp" ofType:#"plist"]]];
I have added the key "ROOKIE" to the Root~iphone.InApp.plist file included in the Settings bundles. I put "ROOKIE" in the "key" column and some string value in the "value" column.
Maybe here is where I am doing something wrong? I don't really understand how the .plist files work and if I am accessing the correct one, or if I am doing it in the right way. I have played around and I can't seem to extract any value from any .plist file so it would seem it's my approach rather than the file?
Any insight would be appreciated.

Create login page only once

Greetings,
I am a new programmer in iphone development . i am making an application with Open Erp i require to a login page which occur only the first time the app is run and also a settings page to change it when required.
I have used NSUSerDefaults to access variables around classes but not sure how to save it and log in automatically the second time the app is run.
I appreciate any help available .
Thank you
You can set object for key in NSUserDefaults as
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:username forKey:#"userName"];
and to get from defaults use -
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *user_name = [userDefaults objectForKey:#"userName"];
when you launch your app check for userdefauls for pre saved username if it is nil then display login view otherwise display view that you want.

Cannot set NSUserDefault ToggleSwitch Value

I have a form within my IPad app that allows the user to configure the application settings (they can also change these settings from the IPad settings app). I have the following code:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:externalAddress forKey:#"firstString_preference"];
[defaults setValue:internalAddress forKey:#"secondString_perference"];
[defaults setValue:isConnectedToDemoString forKey:#"firstToggle_preference"];
[defaults setValue:isConnectedToInternal forKey:#"secondToggle_preference"];
[defaults synchronize];
The firstString_preference and secondString_perference are Textfields in the settings bundle and they save to the settings without issue. My problem is the firstToggle_preference and secondToggle_preference are toggle switches in the settings bundle and I cannot seem to set these at all. They always seem to be set to no.
Does anyone know what I am doing wrong? Should i be using a different method for setting Toggle Switch default values?
Thanks in advance
What type are isConnectedToDemoString and isConnectedToInternal. Unless those are NSNumbers, you should use setBool:forKey:.
Also, while setValue:forKey: works for storing object types to the user defaults, your code would be clearer if you used the NSUserDefaults-native setObject:forKey: method.

How to ship applications with some preferences set to a set value

I have one preference in my application that I want my app to be "shipped" with, how do I do this?
Thanks in advance.
Create an NSDictionary then use
[myDict setObject:#"defaultvalue" forKey:#"mykey"];
for each of your default values. Then use
[[NSUserDefaults standardUserDefaults] registerDefaults:myDict];
It is safe to always do this on startup as it won't overwrite settings. When you go to read the value for your settings via
[[NSUserDefaults standardUserDefaults] objectForKey:#"mykey"];
you will get your default value, or the value that you subsequently set.
Store this preference in your app bundle as part of some set of defaults. When your app launches for the first time (an no saved preferences exist) save this value to NSUserDefaults, the documents directory, or where ever you store your app's preferences.
If there is nothing set in the defaults for a given key, set something. You can do this check whenever your app loads, at any time after the first run, you'll always have an object for your #"MySpecialPref" key.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:#"MySpecialPref"]) {
[defaults setBool:YES forKey:#"MySpecialPref"];
}
You could also ship it with a default SQLite database that contains custom values, and use them in your app