How do I use a default setting from Root.plist? - iphone

I have set up a default setting for my app using Root.plist, giving this a Boolean DefaultValue of YES (ticked checkbox).You can see a screengrab at http://www.infin8design.com/clients/stack/plist.png
However, when my app is first launched, the YES value is not picked up, and the setting is read as a NO (or 0). The user has to manually go into the settings app, turn the toggleswitch off, then turn it back on again for this setting to hold a YES(1) value.
I'm logging the value with appDidFinishLaunching like this.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog(#"%d",[defaults boolForKey:#"include_phrases"]);
SO... my question is... How do I pick up this initial default setting without the user having to set it manually?
Thanks in advance for any suggestions you can offer.
Mark

looks like you need to set the values that you want the key to take when the user switches the parameter on and off. Try adding the following to your plist:
TrueValue String YES
FalseValue String NO

Related

Why does the preferred languages array contain only one item?

I use the following code in my app to print the preferred languages at startup:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSArray* languages = [defaults objectForKey:#"AppleLanguages"];
NSLog(#"%#", languages);
}
The list only contain one item though. Changing the language in the simulator has no effect. The strange thing is that if I start over with a new project the exact same code above prints a full list of languages. How come my app doesn't get the full list of languages? Is there a setting in XCode I might have switched?
I found a solution:
I had most probably played around with the NSUserDefaults and changed the list myself, not knowing it would stick permanently. Removing the app from the simulator and letting XCode upload it again solved the problem. Good to know when running into similar problems.

Alternative to InAppSettingsKit

Is there an alternative to InAppSettingsKit that is simpler? I find myself needing only 20% of what it offers.
How about ESCOZ's QuickDialog library? Seems like a reasonable alternative.
Well, one alternative is to just build your own settings panel with a regular UIViewController and some buttons and switches, etc. and then save the settings using NSUserDefaults, e.g.
- (IBAction)mySettingSwitchAction:(UISwitch *)theSwitch
{
//save the switch setting
[[NSUserDefaults standardUserDefaults] setBool:theSwitch.on forKey:#"myPreferenceName"];
}
then you can load it again anywhere in your app using
BOOL theValueISet = [[NSUserDefaults standardUserDefaults] boolForKey:#"myPreferenceName"];
Values you set in NSUserDefaults are persistent so if the app is closed and opened again they retain their values. You can call synchronize on NSUserDefaults to force it to save/load the values but this happens automatically on app open/close anyway.

Actions on application startup

I'd like that each time my app starts up (possibly even when it's restored from background) it makes an action (for example TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];).
I'd also like that this action (on startup) can be disabled by a switch in the setting bundle of the app. What should I do? Thanks for your attention.
p.s. I apologize if I used incorrect words.. I'm a beginner :)
There are several methods which you can implement in your application delegate which are available in the UIApplicationDelegate protocol.
When your app is first launched applicationDidFinishLaunching is called.
When your app is restored from the background applicationWillEnterForeground is called.
A switch which you add to your setting bundle will have a key, which is an NSString, associated with it. A switch stores a boolean value encoded as an NSNumber in the standard NSUserDefaults under that key. You can read the value of the boolean from the standard user defaults and use it to determine whether to perform the action.
Apples documentation on how to add a settings bundle is here.
In your settings bundle you'll need a toggle switch. The key that you will look up in the standard user defaults is specified by the Key field. The default value for your toggle switch is specified by the DefaultValue field. See here
Here is what you need to do in your applicationDidFinishLaunching method
static NSString *const kTakeActionOnLaunchSettingKey = #"Key";
- (void)applicationDidFinishLaunching
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL doTakeActionOnLaunch = [userDefaults boolForKey:kTakeActionOnLaunchSettingKey];
if (doTakeActionOnLaunch) {
// Do something
}
}
Initialise it in voidDidLoad {} method. For disabling it you can use switch from object libary
It's easy and I used it to make a cool transition from the splash screen to the home page.
You need to put your code inside the AppDelegate m file.
use
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
to run code at startup.
use the following methods to manage the Backgroud <-> Foreground transitions:
- (void)applicationWillResignActive:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application
- (void)applicationWillEnterForeground:(UIApplication *)application
- (void)applicationDidBecomeActive:(UIApplication *)application
Hope this helps you.

Detecting the initial launch of an app using NSUserDefaults

In reference to the following question: iPhone: How do I detect when an app is launched for the first time?
When I looked up NSUserDefaults apple reference, it said that registerDefaults does not store the data onto the disk. In the above question, the app registers the value firstLaunch to YES upon each launch. So each time the app is launched, firstLaunch is overwritten to YES and so the app will always think that this is the app's initial launch.
Am I right on this?
EDIT:
After doing what the tutorial above says, it doesn't always work anyway. I keep relaunching from Xcode and it keep printing out 1 bool value as in its the first launch.
registerDefaults: doesn't overwrite what is already in the defaults, but it sets defaults-for-the-defaults. So if a default is missing it uses a default-default. Confusing, isn't it?
If you've never written a value under the key FirstLaunch, then boolForKey:#"FirstLaunch" will use the value from registerDefaults:. However, if you did previously do setBool:NO forKey:#"FirstLaunch" and later ask for it again with boolForKey: then the value from registerDefaults: is not used.
registerDefaults: doesn't overwrite existing values, it only initializes values that aren't set to any value yet.
Try something like this in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
if ([[[NSUserDefaults standardUserDefaults] valueForKey:#"firstRun"] intValue]==0) {
//do the stuff required at first launch
//after stuff done
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInt:1] forKey:#"firstRun"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
NO first it will be nil then you set it to yes and before setting it to yes check if it's already set to yes or not. So first time userDefaults will return nil if you get something for a key. like
[[NSUserDefaults standardUserDefaults] valueForKey:#"FirstLaunch"];
will return nil on first launch of application.

iPhone and NSUserDefaults

In my viewWillLoad: method I'm currently doing something along these lines:
- (void)viewWillAppear:(BOOL)animated {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ( [defaults boolForKey:#"enabled_preference"] ) {
...
} else {
...
}
[super viewWillAppear:animated];
}
If I build and run the application before opening the preference pane (built using a normal Settings.bundle) then the bool seems to be NO (or more probably nil) rather than the default YES. However if I open the Settings application and look at the application preference pane before I open the application, everything works as expected.
I'm presuming that the application preferences aren't initialized and I should initialise them to the default value (if not already set) in the application delegate. Can someone confirm this? Or am I missing something else blindingly obvious here?
You should provide defaults in your code using -registerDefaults:. This is typically done in an +initialize method for whatever class uses the settings. See Using NSUserDefaults.
Using the initialize method works but I like this other answer on stackoverflow that has code to read the default values from the bundle and uses them to initialize the defaults. That way you don't have to hardcode the default settings in the code, they are in the plist where they belong.