mwt
mwt

Reputation: 523

Loading local html pages in UIWebView

I'm working with a UIWebView loading local (i.e. in the source bundle) html pages, ala Apple's Transweb example. Loading the first page is trivial. However, I have added a second html page and linked to it from the first. Attempting to link to a second page doesn't seem to work.

Anyone know how to make this work?

Thanks.

Upvotes: 1

Views: 1163

Answers (2)

Alessandro Mascolo
Alessandro Mascolo

Reputation: 133

This worked for me:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"html"]]]];

webView is the name of your UIWebView and fileName is the name of your local HTML Page.

Upvotes: 0

drawnonward
drawnonward

Reputation: 53659

I had trouble accessing relative resources when loading a file by path. Try loading the file into memory, either as a string or data, so that you can explicitly pass in a base URL.

NSURL *bundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]];

then

[yourWebView loadHTMLString:yourHTML baseURL:bundleURL];

or

[yourWebView loadData:yourHTML MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:bundleURL];

I used this for accessing image resources, but it may also help with relative links.

Upvotes: 1

Related Questions