Search code examples
xcode4ipad

How to load a local html file into webView on iPad


I have an iPad Master Detail project and I am trying to display html content in a web view which is called detailWebView.

I have set the content of detailItem in the Master View Controller.

self.detailViewController.detailItem=[[_topicsData
                                       objectAtIndex:indexPath.section]
                                      objectAtIndex: indexPath.row];

I have tried many ways to get the extracted information to display in the web view but it will not.

In configureView I have:

NSString *myString=[_detailItem objectForKey:@"url"];
NSString *myNewString=[NSString stringWithFormat:@"%@%@",myString,@".html"];
NSURL *url = [NSURL URLWithString:myNewString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.detailWebView loadRequest:request];

But nothing I try will load the content.

I know I have the correct detail for url because when I try

NSLog(@"detailItem is %@",url);

I get something like myurl.html


Solution

  • You need to add the code in viewDidLoad:

    - (void) viewDidLoad {
        [super viewDidLoad];
    
        self. detailWebView.delegate = self;
    
        NSString *myString=[_detailItem objectForKey:@"url"];
        NSString *myNewString=[NSString stringWithFormat:@"%@%@",myString,@".html"];
        NSURL *url = [NSURL URLWithString:myNewString];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [self. detailWebView loadRequest:request];
    }