WKWebView blank after 'successful' HTTPS NSURLRequest

旧城冷巷雨未停 提交于 2019-12-21 11:03:18

问题


I have created a NSURLRequest (HTTPS)

The delegate callbacks for the WKWebView come back with success, no error.

'decidePolicyForNavigationAction' is provided with the Allow Enum in the decision handler

   @available(iOS 8.0, *)
    func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {

        decisionHandler(.Allow)

    }

and the didReceiveAuthChallenge is handled as such:

@available(iOS 8.0, *)
func webView(webView: WKWebView, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge,
    completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
        let cred = NSURLCredential.init(forTrust: challenge.protectionSpace.serverTrust!)
        completionHandler(.UseCredential, cred)
        print("Did receive auth challenge")
}

as i get no error after'didFinishNavigation' I'm unsure whats going wrong as my WebView is still blank? If i use UIWebView i get the correct webpage showing?

Cheers,


回答1:


To detect errors you should make sure you implement didFailNavigation:withError and didFailProvisionalNavigation:withError.

From the links that work, they seem to be https URLs. The one that does not is an http URL. Implementing the above error methods should tell you what is wrong.

There is a new security feature in iOS9 which fails to load HTTP URLs unless the server conforms to specific set of rules or you setup xcode to override the new feature. You can override the new feature by adding the following into your Info.plist file. Just paste the following at the bottom:

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

This overrides all of the new functionality. You can however perform more specific overrides. The Apple tech note on the changes is here: https://developer.apple.com/library/prerelease/ios/technotes/App-Transport-Security-Technote/index.html




回答2:


I had this problem too but found this solution in a training video:

  1. Add a Dictionary Key to your info.plist with the following name: NSAppTransportSecurity
  2. Create a Boolean subkey to that with the name NSAllowsArbitraryLoads and set to TRUE.

After that it worked here.




回答3:


Make sure your WKWebView frame is set to a positive height in viewDidLoad, because otherwise the webview has no height and does not seem to resize itself after loading:

_webView =[[WKWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height )];


来源:https://stackoverflow.com/questions/32780311/wkwebview-blank-after-successful-https-nsurlrequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!