WKWebView does not load images


Gerriet M. Denkmann
 

[ wkWebView loadHTMLString: htmlString baseURL: nil ];

htmlString contains: …<img alt=“alternate” src=“/path/to/myImage.gif” /> ….

WKWebView just shows a rectangle containing: “alternate”.

WebView shows the picture.

Are there any special steps needed to make WKWebView show images?

Gerriet.


 



On Oct 6, 2017, at 7:58 AM, Gerriet M. Denkmann <g@...> wrote:

htmlString contains: …<img alt=“alternate”  src=“/path/to/myImage.gif” /> ….

WKWebView just shows a rectangle containing: “alternate”.

WKWebView has more security, so it probably disallows filesystem access. Or at least you may need to configure something about the “origin” or “security domain” to enable it. Or setting the baseURL to a “file:” URL might help.

Basically, it’s a security hole to let HTML loaded from the web access the local filesystem. WebView assumes HTML you give it from a string is local/trusted, but it looks like WKWebView doesn’t.

—Jens


Gerriet M. Denkmann
 

On 6 Oct 2017, at 23:29, Jens Alfke <jens@...> wrote:

On Oct 6, 2017, at 7:58 AM, Gerriet M. Denkmann <g@...> wrote:

htmlString contains: …<img alt=“alternate” src=“/path/to/myImage.gif” /> ….

WKWebView just shows a rectangle containing: “alternate”.
WKWebView has more security, so it probably disallows filesystem access. Or at least you may need to configure something about the “origin” or “security domain” to enable it. Or setting the baseURL to a “file:” URL might help.
The only way I can make WKWebView work properly is:

htmlString = …
write htmlString to tempUrl
[ wkWebView loadFileURL: tempUrl allowingReadAccessToURL: common root of tempUrl and imagesUrl ]

I do not like this at all. So I’ll better stick with WebView until WKWebView gets smarter.

Gerriet.


Steve Christensen <punster@...>
 

Is /path/to/myImage.gif a full path or a partial path? I ask because you didn't specify a base URL.

Do the path components have the correct case if the image is on a case-sensitive volume?

On Oct 6, 2017, at 7:58 AM, Gerriet M. Denkmann <g@...> wrote:

[ wkWebView loadHTMLString: htmlString baseURL: nil ];

htmlString contains: …<img alt=“alternate” src=“/path/to/myImage.gif” /> ….

WKWebView just shows a rectangle containing: “alternate”.

WebView shows the picture.

Are there any special steps needed to make WKWebView show images?

Gerriet.


 



On Oct 6, 2017, at 1:52 PM, Steve Christensen <punster@...> wrote:

Is /path/to/myImage.gif a full path or a partial path? I ask because you didn't specify a base URL.

Another thing that just occurred to me — since there is no base URL, there’s nothing to resolve the path relative to. (Yes it’s an absolute path, but that’s still a relative URL.) Try using “file:///path/to/my/image.gif”.

—Jens


Gerriet M. Denkmann
 

On 7 Oct 2017, at 03:52, Steve Christensen <punster@...> wrote:

Is /path/to/myImage.gif a full path or a partial path? I ask because you didn’t specify a base URL.
htmlString actually looks like:

<base href=“file:/absolut/path/to/PicturesFolder/“ />

<img alt=“alternate” src=“./image67.gif" />


I also tried (following the suggestion from Jens):
<img alt=“alternate” src=“file:///absolut/path/to/PicturesFolder/image67.gif” />
Still no picture.


Do the path components have the correct case if the image is on a case-sensitive volume?
Yes, they have. And yes, the volume is case-sensitive.


Also, both:
[ self.webView loadHTMLString: htmlString baseURL: nil ];
and
[ self.webView loadHTMLString: htmlString baseURL: urlTo “/“ ];
give the same result.




On Oct 6, 2017, at 7:58 AM, Gerriet M. Denkmann <g@...> wrote:

[ wkWebView loadHTMLString: htmlString baseURL: nil ];

htmlString contains: …<img alt=“alternate” src=“/path/to/myImage.gif” /> ….

WKWebView just shows a rectangle containing: “alternate”.

WebView shows the picture.

Are there any special steps needed to make WKWebView show images?

Gerriet.


Steve Christensen <punster@...>
 

A couple of things I notice:

1. You seem to be trying several variants on the base or full file URLs without success. Are you generating the paths via NSURL so that they are formatted correctly?

baseURL = [NSURL fileURLWithPath:@"/absolut/path/to/PicturesFolder" isDirectory:YES];

This URL could be passed to -loadHTMLString:baseURL: or you could use baseURL.absoluteString when generating the <base href…>.

2. If your PicturesFolder is contained within a directory in one of the known domains, are you using NSSearchPathForDirectoriesInDomains() to generate that portion of the base URL path and then appending subdirectory components using -stringByAppendingPathComponent: repeatedly to build the full path? If you are making an assumption about where those folders are created then that could be biting you if you made the wrong assumption.

3. The <img> src of "./image67.gif" looks weird to me. I would expect that "image67.gif" would work since the base URL already provides the partial URL to the directory containing that image.


On Oct 7, 2017, at 12:34 AM, Gerriet M. Denkmann <g@...> wrote:

On 7 Oct 2017, at 03:52, Steve Christensen <punster@...> wrote:

Is /path/to/myImage.gif a full path or a partial path? I ask because you didn’t specify a base URL.

htmlString actually looks like:

<base href=“file:/absolut/path/to/PicturesFolder/“ />

<img alt=“alternate”  src=“./image67.gif" />


I also tried (following the suggestion from Jens):
<img alt=“alternate”  src=“file:///absolut/path/to/PicturesFolder/image67.gif” />
Still no picture.


Do the path components have the correct case if the image is on a case-sensitive volume?
Yes, they have. And yes, the volume is case-sensitive.


Also, both:
[ self.webView loadHTMLString: htmlString  baseURL: nil ];
and
[ self.webView loadHTMLString: htmlString  baseURL:  urlTo “/“  ];
give the same result.


On Oct 6, 2017, at 7:58 AM, Gerriet M. Denkmann <g@...> wrote:

[ wkWebView loadHTMLString: htmlString  baseURL: nil ];

htmlString contains: …<img alt=“alternate”  src=“/path/to/myImage.gif” /> ….

WKWebView just shows a rectangle containing: “alternate”.

WebView shows the picture.

Are there any special steps needed to make WKWebView show images?

Gerriet.


Gerriet M. Denkmann
 

I change an iOS app from UIWebView to WKWebView.

1. The Simulator (iPhone SE) complains:
could not create directory “/Volumes/เม่น/Users/gerriet/Library/Developer/CoreSimulator/Devices/77[…]CC/data/Containers/Data/Application/8A2[…]5E/Library/Caches/de.mdenkmann.gThai/WebKit/OfflineWebApplicationCache” for future sandbox extension, error Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “OfflineWebApplicationCache” in the folder “WebKit”."

There certainly is no such thing as: “/Volumes/เม่น/“, but it seems to be working nevertheless.

Both in Simulator and device (iPhone SE) :

2. everything is extremely small: 18 pt. text looks like 6 pt. Probably could be fixed by modifying the css-stuff, multiplying everything by 3.

3. but, same as in macOS, there are no images - just a box with the alt-text.

Gerriet.