Skip to content

Instantly share code, notes, and snippets.

@stefanalund
Created February 17, 2015 20:03
Show Gist options
  • Save stefanalund/1b7a8a0f9a986827ecae to your computer and use it in GitHub Desktop.
Save stefanalund/1b7a8a0f9a986827ecae to your computer and use it in GitHub Desktop.
HTTPS in UIWebView
- (void)webViewDidStartLoad:(UIWebView *)webView
{
_isAuthenticated = NO;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([request.URL.scheme isEqualToString:@"https"]) {
// Handle HTTPS via separate request.
if (!_isAuthenticated) {
NSLog(@"HTTPS: Sending new authentication request");
_failedRequest = request;
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request
delegate:self
startImmediately:NO];
[conn start];
}
return _isAuthenticated;
}
return YES;
}
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSLog(@"HTTPS: willSendRequestForAuthenticationChallenge");
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
NSURL *baseURL = [_failedRequest URL];
if ([challenge.protectionSpace.host isEqualToString:baseURL.host]) {
NSLog(@"HTTPS: Trusting connection to host %@", challenge.protectionSpace.host);
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]
forAuthenticationChallenge:challenge];
} else {
NSLog(@"HTTPS: Not trusting connection to host %@", challenge.protectionSpace.host);
}
}
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"HTTPS: Connection is authenticated");
_isAuthenticated = YES;
[connection cancel];
[self.webRTCWebView loadRequest:_failedRequest];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment