本文介绍了UIWebView 不从协议相对 URL 加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 使用 UIWebView 制作一个应用程序,它打开一个具有协议相关样式的网页 &图片,例如 <img src="//example.com/image.png">(httphttps).

通过您的自定义 NSURLConnection 覆盖页面加载过程,这边.

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

结果:它不会显示图像!虽然 Safari 和其他浏览器可以正确显示它们...

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

推荐答案

我检查了在 WebView 中打开的页面,看到那里有奇怪的请求.它们看起来像普通的 URL,但使用 applewebdata 方案,例如applewebdata://art-u1.infcdn.net/articles_uploads/2/2586/thumb/3Dtouch%20Main-665x.png.耶!它不知道相对 URL //art-u1.infcdn.net/articles_uploads/2/2586/thumb/3Dtouch%20Main-665x.png 的基本"方案并给出它苹果的假计划.所以……问题就在这里:

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!
}

解决方案:只需在启动请求时将初始 URL 保存到某个 m_currentPageUrl,然后将其传递到它应该存在的位置.:)

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. :)

这篇关于UIWebView 不从协议相对 URL 加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:34