Ignoring Invalid Server Certificates with UIWebView [duplicate] Ignoring Invalid Server Certificates with UIWebView [duplicate] ios ios

Ignoring Invalid Server Certificates with UIWebView [duplicate]


Please note: This API is currently unsupported, and should really only be used in a safe testing environment. For further details, take a look at this CocoaNetics article.

[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]]; will allow you to ignore certificate errors. You will also need to add the following to the beginning of your file to grant you access to these private APIs:

@interface NSURLRequest (DummyInterface)+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;@end


Just so everyone knows... the above use of hidden interfaces WILL NOT BE ACCEPTED BY APPLE. They look for use of private APIs and it is NOT an acceptable solution. So, please do not go posting the solution described above around as THE way to fix it because, although it works, it will buy you a rejection in the AppStore. That makes it useless.

What follows is the ACCEPTABLE method of ignoring invalid server certificates. You need to use NSURLConnection and load the data for the webpage manually like so:

...    //Create a URL object.    url = [NSURL URLWithString:urlAddress];    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:requestObj delegate:self];    [connection start];}

And then, in your delegate....

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];}- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])    {        [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];    }    else    {        [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];    }}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {[resultData appendData:data];}- (void)connectionDidFinishLoading:(NSURLConnection *)connection {    NSString *htmlString = [[NSString alloc] initWithBytes:[resultData bytes] length:[resultData length] encoding:NSUTF8StringEncoding];    [webView loadHTMLString:htmlString baseURL:url];}@end

Where resultData is an NSMutableData you instantiated earlier and where url and urlAddress are both things you've instantiated and filled in elsewhere.

Unfortunately, I currently don't know a way to get the actual UIWebView to load a page directly without having a valid certificate.

Yours, GC


It turns out that once the site is authenticated by a cancelled NSURLConnection, the UIWebView can make requests to the site. There is a complete explanation here.


matomo