Search code examples
iosxcodeuiwebviewuisplitviewcontroller

Xcode Master Detail Application UIWebView not loading content


I have a Master Detail Application, on the RootViewController(master) I'm using a tableview to display links, when a user clicks on one of the cells it should populate the detailview with the appropriate webpage.

In the RootViewController here is the code I'm using on the DidSelectRow:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"did select row");
    NSArray* reversedArray = [[objectData reverseObjectEnumerator] allObjects];
    NSDictionary *snapshot = [[NSDictionary alloc] init];
    snapshot = [reversedArray objectAtIndex:indexPath.row];

    DetailViewController *myCont = [[DetailViewController alloc] init];
    NSString *url = snapshot[@"url"];
    [myCont loadPage:url];


}

On the detailview here is the method I have to load the webpage

- (void)loadPage:(NSString*)urlString
{

    NSLog(@"THE URL: %@",urlString);
    NSURLRequest* request=[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    NSLog(@"REQUEST: %@",request);

    //self.myWebView.scalesPageToFit = YES;
    [myWebView loadRequest:request]; 

}

In my Storyboard, I have my UIWebView connected to my DetailViewController for webview delegate and webview outlet.

The page never loads


Solution

  • Have you tried conforming to the UIWebViewDelegate protocol?

    UIWebViewDelegate has these callbacks that will help you debug:

    • webView:shouldStartLoadWithRequest:navigationType:
    • webViewDidStartLoad:
    • webViewDidFinishLoad:
    • webView:didFailLoadWithError:

    All you need to do is find the identifier for the segue and pass the URL string to the DetailViewController when trying to segue into that controller and actually call your loadPage method inside the viewDidLoad() of DetailViewController.

    In the Master-Detail template apple provides you can pass data like this

    ** Take note of the [controller setDetailItem: object] thats where you should pass your URL.

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
       if ([[segue identifier] isEqualToString:@"showDetail"]) {
           NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
           NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
           DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController];
           [controller setDetailItem:object];
           controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
           controller.navigationItem.leftItemsSupplementBackButton = YES;
       }
    }