Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebView blank after 'successful' HTTPS NSURLRequest

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,

like image 998
theiOSDude Avatar asked Sep 25 '15 10:09

theiOSDude


3 Answers

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

like image 104
Rory McKinnel Avatar answered Nov 08 '22 00:11

Rory McKinnel


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.

like image 28
user5388594 Avatar answered Nov 07 '22 23:11

user5388594


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 )];
like image 1
saswanb Avatar answered Nov 08 '22 00:11

saswanb