views:

228

answers:

1

So I'm working with WebKit's - WebView and WebFrame. I use a custom NSURLProtocol to retrieve the HTML for each request from a database. The problem arises from the fact that the links in the HTML are all relative, when they really ought to be absolute. For example, the page

foo/bar.html

May have a link in it that points to

foo/baz.html

The problem is that since the link is relative, the request ends up being for

/foo/foo/baz.html

So far, I've tried to work around this by comparing the two URLs and stripping off the common prefix - in this case 'foo/' - leaving me with foo/baz.html. This doesn't work for all possibilities, however, especially when there are multiple directories in the path. I do this in the "didStartProvisionalLoadForFrame:" method of my WebView's frameLoadDelegate.

Unfortunately, I do not have control over the HTML that I'm displaying, so modifying the links themselves is not an option.

A: 

Try being the main frame's resource load delegate, and implementing webView:resource:willSendRequest:redirectResponse:fromDataSource: to modify the URL being requested. Send relativeString to the request's URL to get the original relative URL, then use -[NSURL initWithString:baseURL:] to create a new URL with the same relative string against the correct base URL.

Peter Hosey
For some reason, [request relativeString] gives me the entire URL, with the current directory already prepended - that is, the relativeString and absoluteString return the same string.
Jeff Barger
So I got it worked out. In webView:resource:willSendRequest:redirecResponse:fromDataSource:, I get the request's URL as well as the URL of the current page. I can get the current directory by removing the last path component, then remove that from the beginning of the request's URL. I'm going to accept this answer, since it at least pointed me in the right direction. Thanks!
Jeff Barger