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.
Related
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.
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.
I want to remember the last screen of my app, means if I reopen my app, app should navigate me to the last screen where I were just before exiting app.
Thanks
Saurabh
Take a look at the NSUserDefaults API.
You could save an object for the current screen in the NSUserDefaults then check for that object on launch.
Create a NSMutableDictionary object when a view loads, and change it's value each time, and store it in the user defaults.
NSMutableDictionary *currentScreen = [[NSMutableDictionary alloc] init];
[currentScreen setObject:#"AboutPage" forKey:#"screen"];
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults setObject:currentScreen forKey:#"lastScreen"];
Then when the app loads, check the user defaults "lastScreen" key and load the appropriate view.
Hope this helps.
in iOS4 it's done automatically, because when you quit your application it's actually enters the background mode and doesn't quit. So when you "launch" it again, it opens at the same place the user left.
Previous versions of iOS don't have this feature and you should take care of it by yourself.
Depending on your application, you may want to think about using the Three20 library. This library has the sort of persistence you are looking for built in.
So I have an iphone app that presents a modal view when it starts if the user has not registered. Once they register, it saves a user ID into NSUserDefaults. When the app starts at a later time, it needs to load this user id in the appdelegate file.
//Registration.m
NSUserDefaults *savedUID = [NSUserDefaults standardUserDefaults];
[savedUID setObject:UID forKey:#"user_id"];
//AuctionTracAppDelegate.m
NSUserDefaults *savedID = [NSUserDefaults standardUserDefaults];
NSString *uidString = [savedID stringForKey:#"user_id"];
My problem is that when I try to load this value at a separate execution, the object I saved is always null. I know for a fact that it is saving the string object correctly. Also, this weird behavior JUST surfaced. This exact code was working earlier, then after working on a totally unrelated file, this code fails. I'm clueless as to what happened. Any hints?
I believe that you need to call the synchronize method.
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