开发者

Loading local html pages in UIWebView

开发者 https://www.devze.com 2023-01-02 16:03 出处:网络
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 linke

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 开发者_JAVA技巧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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消