Ios – WKWebView blank after ‘successful’ HTTPS NSURLRequest

httpsiosswiftuiwebviewwkwebview

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,

Best Answer

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