UIWebView with an absolute CSS path pointer

I have several web pages that are used inside a UIWebView that currently link to resources with both a relative and an absolute path:

<link rel="stylesheet" type="text/css" href="/css/path/mycss.css"/> <img src="myimage.png" /> 

The HTML file itself is located on the path "/page/path/page.html" with myimage.png in the same directory. The same pages are also used for rendering directly in the browser, and ideally I would prefer not to change the paths. On an iPad, the contents of the website are dynamically loaded into the Documents directory for offline viewing. The problem is that there seems to be no way to get UIWebView to handle absolute path resolution. To download the content, I use the following:

 NSData *data = [NSData dataWithContentsOfFile:fullPath]; [self loadData:data MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:baseURL]; 

If I specify baseURL in the "/ page / path" directory, the UIWebView will correctly find myimage.png, but will not find mycss.css. I also tried specifying baseURL in the Documents directory, but this did not resolve the resource. In fact, I could not get UIWebView to fully resolve any absolute paths.

Has anyone been able to get absolute paths to work with UIWebView?

+4
source share
2 answers

Have you tried using something like this (see below) for your CSS path?

 <link rel="stylesheet" type="text/css" href="../../css/path/mycss.css"/> <img src="myimage.png" /> 
0
source

Why do you need absolute path references in your html files?

I do not think this is a good idea because you cannot find out about your future application installation settings for users.

For example, my embedded webpage is in

 file://localhost/var/mobile/Applications/0E8A7BA5-8DB7-4F88-AC69-0E48F9182DD2/MyApp.app/homepage/index.html 

Part 0E8A7BA5-8DB7-4F88-AC69-0E48F9182DD2 changes every time you delete your application!

I think a good way

  • add your site to xCode by selecting "Create folder links for any added folders"
  • have only relative strokes
  • use:

     NSString * urlString = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"homepage"]; NSURL * url = [NSURL fileURLWithPath:urlString isDirectory:NO]; NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:(NSTimeInterval)10.0 ]; 
0
source

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


All Articles