...">

UIWebView baseURL and absolute path

I have an HTML page that is structured as follows:

<html> <head> <link rel="stylesheet" href="/style.css" /> </head> <body> </body> </html> 

It is stored in the directory of my documents:

 /Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/12345-ID/Documents/mysite/index.html 

I also saved style.css in the docs directory:

 /Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/12345-ID/Documents/mysite/style.css 

But now when I try to load html into my webview using this:

 NSURL *test = [NSURL URLWithString:@"file:///Users/username/Library/Application Support/iPhone%20Simulator/5.1/Applications/12345-ID/Documents/mysite/"] [myWebView loadHTMLString:<the content retrieved from index.html> baseURL:test]; 

Css does not load, when I intercept the request into the stylesheet using NSURLProtocol, I see that the request is incorrect. He is trying to request:

File: ///style.css

instead of full baseURL. Now I can solve this by deleting / before / style.css in the html file. But is there an easy way to resolve this in native code without changing the html?

+6
source share
2 answers

Removing a slash was the only solution.

+2
source

Create your path and base url as follows:

 NSString *path = [[NSBundle mainBundle] bundlePath]; NSURL *baseURL = [NSURL fileURLWithPath:path]; [webView loadHTMLString:htmlString baseURL:baseURL]; 

Great blog on this topic:

http://iphoneincubator.com/blog/windows-views/uiwebview-revisited

A SO QA by subject

UIWebView and local css file

+12
source

Source: https://habr.com/ru/post/919190/


All Articles