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

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="", 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 generate the equivalent of a URL pointing to an HTML file that is in the project, so that an can link to that HTML file?

-------------Problems Reply------------

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 "" 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.

Category:uiwebview Views:0 Time:2011-01-28
Tags: uiwebview

Related post

Copyright (C), All Rights Reserved.

processed in 0.148 (s). 11 q(s)