简体   繁体   中英

iOS 8 didReceiveAuthenticationChallenge not getting called for WKWebView Objective C

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]);

}

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM