user987723
user987723

Reputation: 965

read local html file and show on webview

I have been able to read a file called 'exerciseA.htm' from a live url. but now i need to bring the file locally add it to the project and read it from there. I have dragged the file into xcode to add it. Now i need to read its contents to a string, however the following doesnt seem to work:

NSString* exerciseAPage =  [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"exerciseA" ofType:@"htm"] encoding:NSUTF8StringEncoding error:&error];
    [exerciseWebViewA loadHTMLString:exerciseAPage baseURL:nil];

Is this the correct way to load local htm file to a webview?

Upvotes: 1

Views: 1770

Answers (1)

Blounty
Blounty

Reputation: 3358

I do it the following way (Which is basically the same as you specify):

     NSURL *url = [[NSBundle mainBundle] URLForResource:nameOfHtmlFile withExtension:@"html"];
     NSError *error;
     NSString *contentString = [NSString stringWithContentsOfURL:url encoding:NSStringEncodingConversionAllowLossy error:&error];
     NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
     [self.webView loadHTMLString:contentString baseURL:baseUrl];

Hope this helps

Alex

Upvotes: 3

Related Questions