Change application settings through settings bundle while application is running IOS - iphone

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];

Related

NSUserDefault doesn't work on real device(iPhone)

In my App, I am trying to implement SettingView by NSUserDefault. So, I wrote program code as follows:
Method example of saving user setting:
+ (void)setUserAutoPlaySetting:(BOOL)setting {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:setting forKey:kAutoPlaySettingKey];
[userDefaults synchronize];
}
This method is received user setting as BOOL and saves by NSUserDefault.
I confirmed my App save user setting on simulator but not on real Device.
In addition, this method is enable to save NO but disable to save YES on a real device.
Can anyone please tell how can I overcome this problem? Thanks.

NSUserDefaults not saving in iOS 7

I'm having this issue with NSUserDefaults. It is saving the defaults until I kill the app in the background then when I return all the defaults are gone. They are also gone every time a build a new version onto my device. But again, they are there within app so long as I don't kill it in the background. It's working fine in iOS 6. Thoughts?
Take your value in string(or any other as per your requirement) and save the string in user defaults
NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
NSString *str=[NSString stringWithFormat:#"%#",[dicUser objectForKey:#"device_token"]];
[userdefault setObject:str forKey:#"YOURKEY"];
Since you haven't your code, so i am giving example which is perfectly working for me on iOS 7, give it a try. Hopefully works for you too.
For saving boolean value
bool flag = YES;
[[NSUserDefaults standardUserDefaults] setBool:flag forKey:#"flag"];
For retrieving boolean value
bool flag = [[NSUserDefaults standardUserDefaults] boolForKey:#"flag"];
make sure few things like you have declared the userDefaults before using it and if you've taken that in you AppDelegate file then here is a sample code how i am saving values and it works fine so try it this way
APP_DELEGATE.userDefaults = [NSUserDefaults standardUserDefaults];
[APP_DELEGATE.userDefaults setObject:#"unChecked" forKey:#"CheckMark"];
[APP_DELEGATE.userDefaults synchronize];
here APP_DELEGATE is declared like this in my AppDelegate.h file
#define APP_DELEGATE ((AppDelegate*)[[UIApplication sharedApplication] delegate])
hope it helps!

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

Why is NSUserDefaults not saving my values?

Hi I am trying to use NSUserDefaults to save some default values in database. I am able to save the values in the NSUserDefaults (even checked it in NSLog).
Now I need the values in app delegate when the application is restarted. But I am not getting anything in the NSUserDefaults. Following is my code from my class where I save the values in NSUserDefaults:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:appDel.dictProfile forKey:#"dict"];
NSLog(#"%#",[prefs valueForKey:#"dict"]);
Following is my code from App Delegagte:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSLog(#"%#",[prefs valueForKey:#"dict"]);
the above code always returns me null.
Can some one please help me?
If you terminate your app by pressing the home button (in the Simulator or on the device), your NSUserDefaults will get saved.
If you terminate your app by pressing "Stop" in Xcode (in the Simulator or on the device), your NSUserDefaults might get saved, but there's a good chance they won't. NSUserDefaults persists any changes periodically, and if you terminate the process before they've been persisted, they'll be gone. You can force the save by calling:
[[NSUserDefaults standardUserDefaults] synchronize];
Addendum:
In iOS4 (this answer was originally written when iOS3 was the public release), your NSUserDefaults may not get saved when pressing the home button. Manually calling [[NSUserDefaults standardUserDefaults] synchronize] in applicationDidEnterBackground: should ensure that your NSUserDefaults are saved correctly (this should really be a built-in behaviour IMO).
This code works fine for me .
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) {
[standardUserDefaults setObject:myString forKey:#"Prefs"];
[standardUserDefaults synchronize];
}
You didn't say whether you are running on a device or in the simulator, but if you restart the application in the simulator, all preferences will be reset between launches if you launch from Xcode. The preferences will only be preserved if you relaunch from the simulator itself.
In my case I was saving and retrieving a string. When I synchronized after saving and then retrived in another thread, it was not working properly. The problem was solved by synchronizing both after saving and before retreiving.