1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Using a settings .plist to determine what website loads

Discussion in 'iPhone/iPad Programming' started by newtoiphonesdk, Jul 12, 2011.

  1. macrumors 6502a

    I have an app that I want options of viewing audio or video podcast of our podcast. I don't want the clutter of a segmented controller or adding another control in the app so i decided i would do this by adding a settings bundle. I have it working, just with an issue of having to kill the app completely after changing settings to get different podcast to appear. Any suggestions for doing this without having to completely restart app?

    I set up the plist with a PSMultiValueSpecifier and key of defaultTab. I set up an Array under this with Titles of Audio Podcast and Video Podcast, the values I made a string for testing of http://www.google.com and http://www.yahoo.com. In the viewController being used I have this as my viewDidLoad code:

    - (void)awakeFromNib
    	NSString *path = [[NSUserDefaults standardUserDefaults] objectForKey:@"defaultTab"];
    	Reachability *reachability = [Reachability reachabilityWithHostName:@"google.com"];
    	NetworkStatus status = [reachability currentReachabilityStatus];
    	if (status == ReachableViaWiFi) {
    		[sermons loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:path]]];
    	else {
    		UIAlertView *cancelled = [[UIAlertView alloc] initWithTitle:@"Wifi Required" message:@"To listen to the sermons, you must be connected to a Wifi Network.  After connecting to a Wifi Network, press the 'Reload Sermons Page' button (located in Sermons tab) to try again.  You may also download sermons via iTunes app from our official Podcast." delegate:self cancelButtonTitle:@"Open in iTunes" otherButtonTitles:@"Dismiss", nil];
    		[cancelled show];
    		[cancelled release];
    	timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(tick) userInfo:nil repeats:YES];	
    This works for me, and it will use google.com as default, and I can change it in the Settings window, but here is where I run into a bit of an issue. If I do not kill the app in the multi-tasking bar, it won't change after I change it in the settings app. Once I completely kill the app, and restart it, it will load the different view. Is there a way I can make it change without having to kill app completely?
  2. Moderator


    Staff Member

    awakeFromNib is only called when the object is de-serialized from the nib/xib file. This does not happen every time the app is shown/brought back from the background. Look at using a method of the UIViewController that will be called every time the view is displayed or an app delegate method that will be called when the application did become active.
  3. macrumors 6502a

    I had thought about using the viewWillAppear method, but then, everytime the user navigated away from that Controller and came back, it would go back to the default. As this webview would be showing a podcast, there could be a lot of issues if say, the user wanted to listen to a podcast, did something else, and wanted to pause it, but when they open the controller, it just goes right back to the original site.
  4. Moderator


    Staff Member

    It would only do that if you programmed it to do that. There is nothing stopping you having a variable that holds the currently selected website from the settings.plist and in viewWillAppear check whether the currently selected on in settings.plist matches the variable. If it does don't change anything, if it does not update your view.
  5. macrumors 6502a

    Let me make sure I understand what you are saying. The viewWillAppear can be programmed to check if the value in the settings.plist has changed, and if it has it would reload to match the value, and otherwise do nothing?
  6. Moderator


    Staff Member

    Yes, you can program it to do that if you track what the existing setting is. It's up to you: programming is a creative discipline. Be creative.
  7. macrumors 6502a

    Thanks. This is my first time working with a custom plist file in one of my apps. How do you track the values of this?
  8. Moderator


    Staff Member

    Read what I said above: store the value in a variable. Ensure the scope of the variable is such it will persist for as long as you need it. There is absolutely nothing plist specific in this at all.
  9. macrumors 6502a

    I don't mean to be a pain, and I really am trying stuff out, but I am unsure how to store the value in a variable. I do understand the concept that I can store the value, and then in the viewWillAppear create a NSString for the objectforkey and then compare the two, and if they do not match, have it load the website for whatever the existing objectforkey value is. What I am lost at is how to store the original value in a variable. Am I making sense? Sometimes I can ramble and not get what I mean out.
  10. macrumors 603

    To store something in a variable, first you need a variable. So decide what variable name you want, what its type should be, and where it should be placed in the code. Then write the code to do that.

    If you don't know where to place the variable, think about your options: it could be an instance variable in an object, a static variable outside any object, or a local variable in a method. The correct one to use depends on what lifetime you need for whatever gets stored in the variable. So think about the lifetime and where you need to access it from (called the variable's "scope").
  11. newtoiphonesdk, Jul 12, 2011
    Last edited: Jul 12, 2011

    macrumors 6502a

    I have never done anything like this before, so I am completely unsure where it would need to be placed. My guess would be an instance variable in an object so that I could store it in the viewController?

    On a slightly different note, the first time I run the app, nothing loads in the webview at all. I believe this may be due to an error in my plist. Could you look at this and see if the DefaultValue needs to be changed to something else?
    			<string>Podcast Type Selector</string>
    			<string>Podcast Type</string>
    				<string>Audio Podcast</string>
    				<string>Video Podcast</string>

Share This Page