Has anybody found a way to load HTTPS pages with an invalid server certificate using UIWebView?

后端 未结 2 475
忘了有多久
忘了有多久 2020-12-14 04:18

If a user attempts to load a https web page in Mobile Safari and the server\'s certificate validation check fails (its expired, revoked, self-signed etc.) then the user is p

相关标签:
2条回答
  • 2020-12-14 04:41

    From the horse's mouth:

    "UIWebView does not provide any way for an app to customize its HTTPS server trust evaluations. It is possible to work around this limitation using public APIs, but it is not easy. If you need to do this, please contact Developer Technical Support (dts@apple.com)

    Source: http://developer.apple.com/library/ios/#technotes/tn2232/_index.html

    0 讨论(0)
  • 2020-12-14 04:42

    I found out how to do this:

    1) When the page is loaded it will fail, thus add something like the following to didFailLoadWithError:

    - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
        if ([error.domain isEqualToString: NSURLErrorDomain])
        {
            if (error.code == kCFURLErrorServerCertificateHasBadDate        ||
                error.code == kCFURLErrorServerCertificateUntrusted         ||
                error.code == kCFURLErrorServerCertificateHasUnknownRoot    ||
                error.code == kCFURLErrorServerCertificateNotYetValid)
            {
            display dialog to user telling them what happened and if they want to proceed
    

    2) If the user wants to load the page then you need to connect using an NSURLConnection:

    NSURLRequest *requestObj = [NSURLRequest requestWithURL:self.currentURL     cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0];
    self.loadingUnvalidatedHTTPSPage = YES;
    [self.webView loadRequest:requestObj];
    

    3) Then make this change to shouldStartLoadWithRequest

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
    {
        if (self.loadingUnvalidatedHTTPSPage)
        {
            self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
            [self.connection start];
            return NO;
        }
    

    4) Implement the NSURLConnectionDelegate as:

    - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
    {
        SecTrustRef trust = challenge.protectionSpace.serverTrust;
        NSURLCredential *cred;
        cred = [NSURLCredential credentialForTrust:trust];
        [challenge.sender useCredential:cred forAuthenticationChallenge:challenge];
    }
    
    
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
    {
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:self.currentURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0];
        self.loadingUnvalidatedHTTPSPage = NO;
        [self.webView loadRequest: requestObj];
        [self.connection cancel];
    }
    

    It all seems to work fine.

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