WKWebView不会加载某些URL

时间:2019-10-02 05:15:31

标签: ios swift wkwebview

info.plist

我将所有任意载荷设置为True。

enter image description here

大多数URL可以很好地加载,有些则不能。

let webView = WKWebView(frame: CGRect(x: 0,
                                          y: 0,
                                          width: view.frame.size.width,
                                          height: view.frame.size.height))
    webView.navigationDelegate = self
    webView.isHidden = true
    activityView.center = view.center
    activityView.style = UIActivityIndicatorView.Style.gray
    activityView.startAnimating()
    view.addSubview(activityView)
    view.addSubview(webView)
    let url = URL(string: urlString ?? "")
    webView.load(URLRequest(url: url!))


// MARK: - Public Instance Method
extension DealsWebView {
func webView(_ webView: WKWebView,
             didFinish navigation: WKNavigation!) {
    webView.isHidden = false
    activityView.stopAnimating()
}

func webView(_ webView: WKWebView,
             didReceive challenge: URLAuthenticationChallenge,
             completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
    if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
        let cred = URLCredential(trust: challenge.protectionSpace.serverTrust!)
        completionHandler(.useCredential, cred)
        print("[WEBVIEW] .useCredential: ")
    } else {
        completionHandler(.performDefaultHandling, nil)
        print("[WEBVIEW] .performDefaultHandling: ")
    }
 }
}

例如https://apple.com可以很好地加载。 但是,如果我尝试加载:http://indigo.ca WebView甚至不会调用didFinish

1 个答案:

答案 0 :(得分:0)

您需要在info.plist文件中进行此更改:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>example.com</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
    </dict>
</dict>