info.plist
我将所有任意载荷设置为True。
大多数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
答案 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>