0

Why some https links are not loading the WebView. I can see that some https url are loading perfectly in the safari browser, but when I try to load the same url in the webview, it is not loading. May be because the https has self-signed certificate ?? Can't we load the url in the WebView which has self-signed certificate ?

EDIT: Now I am able to call

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace 
{
    return YES;
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{
    [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}

Still my https url is not loaded in the WebView.

greut
  • 4,305
  • 1
  • 30
  • 49
Cyril
  • 1,216
  • 3
  • 19
  • 40

1 Answers1

0

Try this :

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace 
{
  return YES;
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSArray *trustedHosts = [NSArray arrayWithObjects:@"mytrustedhost",nil];

if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
    if ([trustedHosts containsObject:challenge.protectionSpace.host]) {
    [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
    }
}
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • Should I add NSURLConnectionDelegate in the .h file . So far in .h file UIViewController are added. – Cyril May 29 '12 at 04:38
  • Add in .m file. Let me know if any problem – Paresh Navadiya May 29 '12 at 04:45
  • I have added the two methods in my .m file. I have kept breakpoint,It is not getting called. :( – Cyril May 29 '12 at 04:48
  • I am processing the request using the following code ,NSURL* urlLink= [NSURL URLWithString:url]; NSURLRequest* request=[NSURLRequest requestWithURL:urlLink]; [self.webView loadRequest:request]; – Cyril May 29 '12 at 04:57
  • Please check my question again, I have updated my code as you said. Waiting for your reply – Cyril May 29 '12 at 05:23
  • check edited answer and refer [this](http://stackoverflow.com/questions/9122761/ios-5-nsurlconnection-to-https-servers) link – Paresh Navadiya May 29 '12 at 05:31
  • Am almost there. Thank you so much.I can able to get the NSData and I am displaying those in the WebView. But the thing is it is bypassing the authentication. If I access my https url, it open a dialog box for authentication , but now it is skipping that area. Any idea ?? Please – Cyril May 29 '12 at 06:02
  • Simply adding the canAuthenticateAgainstProtectionSpace and didReceiveAuthenticationChallenge methods , it is not displaying the https page in webview.SO i added the NSURLConnection delegate methods to get the NSData and calling the loadData method. But it is not working properly. – Cyril May 29 '12 at 06:49
  • Looks like the didReceiveAuthenticationChallenge and canAuthenticateAgainstProtectionSpace method is being called then why those are not loaded in the webview. ?? – Cyril May 29 '12 at 06:50
  • For your Information, Now those 2 methods are loading my office mail https url (self-signed), which will ask for authentication in the webpage. Thank you. But I do know one https url , which will pop-up a dialog box instead of webpage for authentication. This is not displayed :( – Cyril May 29 '12 at 07:08