How to call https url in uiwebview?

前端 未结 1 893
情歌与酒
情歌与酒 2020-12-06 02:05

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

Error is:

NSURLConnection/CFURLConnection HTT

相关标签:
1条回答
  • 2020-12-06 02:55

    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

    0 讨论(0)
提交回复
热议问题