Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call https url in uiwebview?

Tags:

ios

ssl

uiwebview

I have an HTTPS url. It is loading in iPhone Safari, but didn't work in UIWebView.

Error is:

NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)

How can I fix this problem?

like image 801
user2789934 Avatar asked Dec 04 '13 02:12

user2789934


1 Answers

I have explained below how to access https url in UIWebview it will help you clear the problem because it works for me.

Calling http is same as https url.

If, however, you're using a self-signed certificate, it's necessary to add some additional code.Because by default iOS will reject all untrusted https connections.

Restricting untrusted connections is very good default behaviour and any disabling of this is highly risky.So we will show an alert as we're going to bypass the default behaviour.

-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace;

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;

These two above methods allows us to provide our own authentication mechanism for trusted connections

#import "ClassCon.h"
//  For now, I've hard coded the IP address of my trusted server.
#define TRUSTED_HOST @"192.168.1.2"


@implementation ClassCon {
    NSMutableData *contentData;
    NSURLConnection *conn;
}

-(void) loadContent {
    contentData = [NSMutableData data];
    NSString *contentURL = @"our url";
    conn = [[NSURLConnection alloc] initWithRequest:
            [NSURLRequest requestWithURL:[NSURL URLWithString:contentURL]] delegate:self];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [contentData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Bad: %@", [error description]);
    ContentResponse *response = [[ContentResponse alloc]initWithRc:-999 andDescr:@"error" andAction:0];


    conn = nil;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        NSString *loadedContent = [[NSString alloc] initWithData:
                                     contentData encoding:NSUTF8StringEncoding];
        NSLog(@"Loaded content: %@",loadedContent);

    }

// ------------ ByPass ssl starts ----------
-(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])) {
        if ([challenge.protectionSpace.host isEqualToString:TRUSTED_HOST]) {
            NSLog(@"Allowing bypass...");
            NSURLCredential *credential = [NSURLCredential credentialForTrust:
                                           challenge.protectionSpace.serverTrust];
            [challenge.sender useCredential:credential
                 forAuthenticationChallenge:challenge];
        }
    }
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
// -------------------ByPass ssl ends




@end

Hope this helps

like image 100
Durai Amuthan.H Avatar answered Nov 06 '22 15:11

Durai Amuthan.H