Strange behavior with WKWebView's loadHTMLStri

2020-07-26 13:41发布

问题:

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.

For example:

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];

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.

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.

My OSX version: 10.11 public beta 5; xCode version: 7.0 beta.

回答1:

If you are developing for iOS9, you must read about NSAppTransportSecurity. Here is an SO question / answer to get you started.

My own info.plist file includes:

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

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.

Glad this helped!