2

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

}

user2212736
  • 93
  • 1
  • 7
  • 1
    did you find any solution for didReceiveAuthenticationChallenge? In my case also it never gets called. Also as its in deprecated in iOS8, how can we achieve this in iOS8? – harshit2811 Jul 23 '15 at 09:44
  • I couldn't find any solution so I changed certificate on server with a valid one to get rid of this error. – user2212736 Jul 24 '15 at 07:21

2 Answers2

1

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

jirka26
  • 11
  • 1
  • Can you please add references confirming the fix? – Phani Sep 22 '15 at 15:55
  • @Phani I filed a bug to Apple, which is fixed now. Unfortunately, Apple's bug reports cannot be seen by other users. I have verified the fix in my project, it works correctly in iOS 9.0 (XCode 7.0). I'm sorry I cannot share my project here, but this scenario is really easy to test with a simple WKWebView and the code from the original question. – jirka26 Sep 22 '15 at 16:27
0

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;
Subbu
  • 2,138
  • 14
  • 20