Chris Byatt
Chris Byatt

Reputation: 3819

iOS Load Web Page in UIWebView from Another View Controller

I have a tabbed application, with a tab that contains a UIWebView and a second tab with a Table View Controller.

I'm trying to make it so that when I click on a row in the table it takes me to my other tab and loads a web page.

Here's my storyboard: My Storyboard

This is in my implementation for the web view:

-(void)loadAViewer:(NSURL*)viewerURL
{
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:viewerURL];
    [sfnViewer loadRequest:requestObj];
}

And then in the table view controller:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Get the URL from plist
    ...

    //Generate url, pass it in and switch to web view tab
    viewerURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@/viewer?username=%@&pw=%@&%@", web, loc, user, pass, params]];

    ViewerFirstViewController *viewerWebView = [[ViewerFirstViewController alloc] init];
    [viewerWebView loadAViewer:viewerURL];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.tabBarController setSelectedIndex:0];

}

Currently nothing happens. I'm taken to the other tab but with a blank UIWebView - no page loads but it doesn't crash either.

//Edit - Changes made based on answer from Steve

Based on this I've added @property (strong, nonatomic) NSURL viewerUrl; in my .h file for the UIWebView tab.

I've synthesized it in the .m and added this to my viewDidLoad method.

NSURLRequest *requestObj = [NSURLRequest requestWithURL:viewerURL];
[sfnViewer loadRequest:requestObj];

In the Table View Controller I'm now doing this.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Get the URL from plist
    ...

    //Generate url, pass it in and switch to web view tab
    viewerURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@/viewer?username=%@&pw=%@&%@", web, loc, user, pass, params]];

    ViewerFirstViewController *viewerWebView = [[ViewerFirstViewController alloc] init];
    [viewerWebView setViewerUrl:viewerURL];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.tabBarController setSelectedIndex:0];

}

Still to no avail.

Upvotes: 0

Views: 1497

Answers (1)

Steve Wilford
Steve Wilford

Reputation: 9002

It's probably not loading because when you call loadAViewer: the view itself hasn't been loaded yet so the sfnViewer outlet won't be hooked up to the UIWebView in your storyboard.

You can confirm this by checking if sfnViewer is nil in loadAViewer:.

If this is the case you will have to define an NSURL property on the ViewerFirstViewController and assign to that instead of calling loadAViewer:. Then in the viewWillAppear: method you should load the URL into sfnViewer.

However this will result in the web view being reloaded each time the tab is accessed, you could avoid this by checking if viewerURL is different from the URL currently in use in the web view: [sfnViewer.request.URL isEqual:self.viewerURL], if it is the same then don't create/load the request.

Upvotes: 1

Related Questions