iOS 8 didReceiveAuthenticationChallenge not getting called for WKWebView Objective C

6k views Asked by At

I am new to iOS development. I have a project where I have to use WKWebView instead of UIWebView. Its a simple webpage with javascript to Objective-C and other way round integration. It is working fine except when I try to open a server with self-signed certificate. On Safari it shows a dialog box where we can choose to continue. However on my Application I cannot bypass this.

For some reason I have to run it with self-signed certificate.

The Delegate method didReceiveAuthenticationChallenge is never called. Other delegate methods are working fine. I know didReceiveAuthenticationChallenge is depreciated in iOS8 but can someone please tell me the workaround for this. As I am a newbie so a complete working delegate method and/or any other changes in the code will be highly appreciated.

NSURL *url = [NSURL URLWithString:@"https://mywebsite.com/Default.aspx"];

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

[[self webView] loadRequest:request];

}

// And the delegate method that is not getting called is 
- (void)webView:(WKWebView *)webView
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
    SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
    CFDataRef exceptions = SecTrustCopyExceptions(serverTrust);
    SecTrustSetExceptions(serverTrust, exceptions);
    CFRelease(exceptions);

completionHandler(NSURLSessionAuthChallengeUseCredential,
                  [NSURLCredential credentialForTrust:serverTrust]);

}

2

There are 2 answers

0
Subbu On

In the code snippet you posted, i didn't see you were setting the class as the navigationDelegate of the webview..

Your view controller should implement the protocol WKNavigationDelegate and you need to add the below snippet for the delegate to be invoked..

[self webView].navigationDelegate = self;
2
jirka26 On

This has been confirmed as a bug by Apple. It has been fixed in iOS9.