Application Preferences - not up to date right away.

Discussion in 'iOS Programming' started by IDMah, May 5, 2012.

  1. IDMah macrumors 6502


    May 13, 2011
  2. ArtOfWarfare macrumors G3


    Nov 26, 2007
    My guess is that this tutorial may be dated and not take multitasking into account (although iOS 4 was released in mid 2010 so it seems like a 2011 tutorial aught to consider it...)

    Anyways, in the application delegate, you don't want to be looking for applicationDidFinishLaunching (as that only happens if the app wasn't running in the background.) You would want applicationDidBecomeActive, instead.
  3. IDMah thread starter macrumors 6502


    May 13, 2011
    thanks. I did put it there.
    // in AppDelegate.m //
    - (void)applicationDidBecomeActive:(UIApplication *)application {
        // checks loads and creates plist if not available 
       [self prefs];
    but I think it may be a simulator thang.
    but it works most of time.
  4. jnoxx macrumors 65816


    Dec 29, 2010
    Aartselaar // Antwerp // Belgium
    I didn't click the links, but when it isn't up to date, that mostly means you forget to call "synchronize" on your userdefaults.
  5. IDMah, May 7, 2012
    Last edited: May 7, 2012

    IDMah thread starter macrumors 6502


    May 13, 2011
    the code.

    So now. For some reason it's exiting because it can't find the root.plist?
    Here's what I use to make and check the prefs..

    	//It is here that we set the defaults
    	NSString *textValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"SystemMode"];
    	NSString *commandValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"commandstr"];
    	//If the first value is nil, then we know that the defaults are not set.
    	if(textValue == nil)
    		//Get the bundle path
    		NSLog(@"no prefences found");
    		NSString *bPath = [[NSBundle mainBundle] bundlePath];
    		NSString *settingsPath = [bPath stringByAppendingPathComponent:@"Settings.bundle"];
    		NSString *plistFile = [settingsPath stringByAppendingPathComponent:@"Root.plist"];
    		//Get the Preferences Array from the dictionary
    		NSDictionary *settingsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistFile];
    		NSArray *preferencesArray = [settingsDictionary objectForKey:@"PreferenceSpecifiers"];
    		//Temporary Variables
    		NSDictionary *item;
    		NSString *textEntry_Key;
    		NSString *readOnly_Key;
    		NSString *toogle_Key;
    		NSString *slider_Key;
    		NSString *colors_Key;
    		//Loop through the array
    		for(item in preferencesArray)
    			//Get the key of the item.
    			NSString *keyValue = [item objectForKey:@"Key"];
    			//Get the default value specified in the plist file.
    			id defaultValue = [item objectForKey:@"DefaultValue"];
    			if([keyValue isEqualToString:@"textEntry_key"])
    				textEntry_Key = defaultValue;
    			if([keyValue isEqualToString:@"readOnly_key"])
    				readOnly_Key = defaultValue;
    			if([keyValue isEqualToString:@"toogle_key"])
    				toogle_Key = defaultValue;
    			if([keyValue isEqualToString:@"slider_key"])
    				slider_Key = defaultValue;
    			if([keyValue isEqualToString:@"colors_key"])
    				colors_Key = defaultValue;
    		//Now that we have all the default values.
    		//We will create it here.
    		NSDictionary *appPrerfs = [NSDictionary dictionaryWithObjectsAndKeys:
    								   textEntry_Key, @"textEntry_key",
    								   readOnly_Key, @"readOnly_key",
    								   toogle_Key, @"toogle_key",
    								   slider_Key, @"slider_key",
    								   [NSNumber numberWithInt:1], @"slider_key",
    								   [NSNumber numberWithInt:1], @"colors_key",
    		//Register and save the dictionary to the disk
    		[[NSUserDefaults standardUserDefaults] registerDefaults:appPrerfs];
    		[[NSUserDefaults standardUserDefaults] synchronize];
    		NSLog(@"Pref Files: %@",textValue);
    		NSLog(@"Pref file Command: %@",commandValue);
    it's triggering but may not be writing. Cuz when I exit go to the Settings change something. It works fine?
    It also only happens on the very first run of the app.

    Any Ideas?? I'm so close to finishing this darn app. Just want it to work.


    ps. is there a way to add a file to my bundle? I thought that was what the happening above.
  6. IDMah thread starter macrumors 6502


    May 13, 2011
    small change.

    Well it was getting triggered but.. this seems to have fixed it.

    if([textValue isEqual:nil]) 
    makes some sense.. I guess.

    hope this helps some one..

Share This Page