UIWebView doesn't load images from protocol-relative URL
Asked Answered
A

1

6
  1. Make an app with UIWebView which open a webpage with protocol-relative styles & images, such as <img src="//example.com/image.png"> (examples for http or https).

  2. Override the page loading process via your custom NSURLConnection, this way.

Result: it won't display the images! Though, Safari and other browsers show them correctly...

Aphid answered 24/9, 2015 at 13:55 Comment(0)
A
4

I inspected the page opened in WebView and saw weird requests there. They looked like regular URLs, but with applewebdata scheme, e.g. applewebdata://art-u1.infcdn.net/articles_uploads/2/2586/thumb/3Dtouch%20Main-665x.png. Yay! It doesn't know the "base" scheme for the relative URL //art-u1.infcdn.net/articles_uploads/2/2586/thumb/3Dtouch%20Main-665x.png and gives it that Apple's fake scheme. So... the problem is here:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [m_webView loadData:webdata
               MIMEType:@"text/html"
       textEncodingName:@"UTF-8"
                baseURL:nil]; /// WTF!
}

Solution: just save the initial URL to some m_currentPageUrl when starting the request, and then pass it to where it's meant to be. :)

Aphid answered 24/9, 2015 at 13:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.