PDA

View Full Version : Using a settings .plist to determine what website loads




newtoiphonesdk
Jul 12, 2011, 11:08 AM
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]]];
reload.hidden=YES;
sermons.hidden=NO;
}

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];
reload.hidden=NO;
sermons.hidden=YES;
}

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?



robbieduncan
Jul 12, 2011, 11:14 AM
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.

newtoiphonesdk
Jul 12, 2011, 11:19 AM
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.

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.

robbieduncan
Jul 12, 2011, 11:22 AM
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.

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.

newtoiphonesdk
Jul 12, 2011, 11:45 AM
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.

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?

robbieduncan
Jul 12, 2011, 12:24 PM
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?

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.

newtoiphonesdk
Jul 12, 2011, 12:40 PM
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.

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?

robbieduncan
Jul 12, 2011, 12:43 PM
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?

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.

newtoiphonesdk
Jul 12, 2011, 01:08 PM
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.

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.

chown33
Jul 12, 2011, 01:30 PM
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").

newtoiphonesdk
Jul 12, 2011, 01:47 PM
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").

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?
<dict>
<key>StringsTable</key>
<string>Root</string>
<key>PreferenceSpecifiers</key>
<array>
<dict>
<key>Type</key>
<string>PSGroupSpecifier</string>
<key>Title</key>
<string>Podcast Type Selector</string>
</dict>
<dict>
<key>Type</key>
<string>PSMultiValueSpecifier</string>
<key>Title</key>
<string>Podcast Type</string>
<key>Key</key>
<string>defaultTab</string>
<key>DefaultValue</key>
<string>0</string>
<key>Titles</key>
<array>
<string>Audio Podcast</string>
<string>Video Podcast</string>
</array>
<key>Values</key>
<array>
<string>http://www.316apps.com/iphonesermons.html</string>
<string>http://www.yahoo.com</string>
</array>
</dict>
</array>
</dict>
</plist>