开发者

Can I link to an HTML file in my project from a UIWebView?

开发者 https://www.devze.com 2023-02-07 09:15 出处:网络
If I load a string containing HTML into a UIWebView, and that string containsobjects (hyperlinks) that are relative to that string, i.e. , where there is some object with id \"something,\" then the li

If I load a string containing HTML into a UIWebView, and that string contains objects (hyperlinks) that are relative to that string, i.e. , where there is some object with id "something," then the link works - click on it and the web view jumps to the referenced object.

What I want is to get navigation to a different file in my project, in other words as though the path to the different file were a URL.

I have found that if the href IS a URL, such as href="http://www.amazon.com", then the link works.

If I put the name of a file, OR the [NSBundle mainBundle] pathForResource: ] of that name, in the href, then the link does not work.

Is there some way I can generat开发者_JAVA技巧e the equivalent of a URL pointing to an HTML file that is in the project, so that an can link to that HTML file?


I found a solution at this link:

How to use Javascript to communicate with Objective-c code?

Essentially, the solution is to implement the UIWebViewDelegate protocol's shouldStartLoadWithRequest method, and "trap" a particular value of scheme. So my links, instead of saying something like:

<a href="http://someplace.location">

are like:

<a href="mylink://#filename.ext">

By catching attempts to load anything with scheme "mylink," I can use:

[[request URL] fragment]

within shouldStartLoadWithRequest, and get the filename.ext. I then release my previous UIWebView, load in the contents of the specified file, and make that the contents of a new UIWebView. The effect is that the links work with normal appearance, even though they are being implemented with my code. I return NO because I don't want the usual loading to take place. If the scheme is NOT mylink, I can return YES to allow normal operation.

Regrettably, I still have no way to jump TO a fragment within a web view. In linking to a real URL, you can say something like "www.foo.org#page50" and jump straight to wherever an object on the new page has an id of "page50." With my method, I can only go to the top of the page.

This is also not going to give me a "go-back" function unless I record the filenames and implement it myself.

0

精彩评论

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