问题描述
我正在开发一个mac应用程序项目。使用WKWebView的loadHTMLString方法,我能够构建一个网页视图,除了图像加载部分,它工作良好。
I am working on a mac app project.Using WKWebView's loadHTMLString method,I am able to construct a web page view,it works well except for the image loading part.
例如:
NSString *string1 = @"<img src=\"https://apppie.files.wordpress.com/2014/09/photo-sep-14-7-40-59-pm_small1.jpg\">"
[pWebView loadHTMLString: string1 baseURL: nil];
这很好。但是一旦我将 https:
更改为 http:
,则网页损坏,图片不再显示。我在Firefox浏览器上检查过这两个网址,他们都很好。我不明白为什么会发生这种情况。
That works well. But once I change https:
to http:
,the web page is broken, image is not showing anymore. I have checked both urls on Firefox browser,they all go well. I don't understand why this is happening.
然后我尝试将 http:
替换为 https:
,网页的大部分图像正常加载,但仍然不是所有的(再次,我在Firefox中检查它,所有图像显示没有问题),因此,我非常确定WKWebView配置有什么问题,我该如何解决这个问题?非常感谢。
And then I try to replace all http:
with https:
,most of images of the web page load properly, but still not all of them.(Again,I double check it in Firefox,all images show up without problem),so I am pretty sure there is something wrong with WKWebView configuration,how can I fix that?Thanks a lot.
我的OSX版本:10.11 public beta 5;
My OSX version: 10.11 public beta 5; xCode version: 7.0 beta.
推荐答案
如果您正在开发iOS9,您必须阅读NSAppTransportSecurity。 /答案让你开始。
If you are developing for iOS9, you must read about NSAppTransportSecurity. Here is an SO question / answer to get you started.
我自己的info.plist文件包括:
My own info.plist file includes:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
这可能比我应该开放得多,但随着时间的推移,到我需要的特定网址。如何做到这一点也显示在链接的q / a。
This is probably much more open than I should be, but I'll change over time to limit access to specific URLs that I need. How to do this is also shown in the linked q/a.
很高兴帮助了!
这篇关于使用WKWebView的loadHTMLString方法的奇怪行为。图像有时不加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!