Question

In my app, I scan a webpage, extract certain parts, and build an HTML String to load in a webview. Because of this, I have to set a base URL for links that can be clicked on. I currently use:

[webView loadHTMLString:self.html baseURL:[NSURL URLWithString:@"http://www.ocacademy.org/ocacademy"]];

The issue is that subsequent links only have http://www.ocacademy.org/ in front of them instead of with the subdirectory ocacademy. Any thoughts as to what is messing up here?

Was it helpful?

Solution

You need to either append a / or more explicitly /name_of_html_file.html to the base URL.

Any trailing filename in the base URL will be stripped off when constructing a relative URL (ocacademy in this case).

So given a base URL of http://www.ocacademy.org/ocacademy and a relative reference to image.png, the ocacademy is stripped off to give a parent directory http://www.ocacademy.org/ and the resulting URL will be http://www.ocacademy.org/image.png.

If the base URL is http://www.ocacademy.org/ocacademy/.* then the .* is stripped off before constructing the URL and you will get http://www.ocacademy.org/ocacademy/image.png (what you want).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top