2014-02-12 48 views
11

我正嘗試將一個具有自簽名證書的HTTPS網頁加載到UIWebView中。使用諸如this onethis one的提示,它可以在iOS 6下工作。在iOS 7中也是如此。繞過iOS 7上自簽名證書的kCFStreamErrorDomainSSL錯誤

根據鏈接到堆棧溢出問題,我還使用NSURLConnection來首先嚐試並取得過去自簽名證書 - 甚至在嘗試在UIWebView中加載URL之前。

當iOS的7嘗試一樣,我得到以下錯誤:

2014-02-12 16:00:08.367 WebView[24176:5307] NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)

2014-02-12 16:00:08.370 WebView[24176:70b] An SSL error has occurred and a secure connection to the server cannot be made.

是否有變通得到這個在iOS的7工作?目前我正在使用first example

+0

嘗試使用此實施UIWebViewDelegate方法:http://stackoverflow.com/a/15074358/1694129 –

+0

基本上是http://stackoverflow.com/questions/11573164的副本,現在也有一個解決方案寫在swift – spirographer

回答

17

請按照鏈接:

in UiWebView - NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -108)

BOOL _Authenticated; 
NSURLRequest *_FailedRequest; 
#pragma UIWebViewDelegate 

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { 
    BOOL result = _Authenticated; 
    if (!_Authenticated) { 
     _FailedRequest = request; 
     NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
     [urlConnection start]; 
    } 
    return result; 
} 

#pragma NSURLConnectionDelegate 

-(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { 
     NSURL* baseURL = [NSURL URLWithString:@"your url"]; 
     if ([challenge.protectionSpace.host isEqualToString:baseURL.host]) { 
      NSLog(@"trusting connection to host %@", challenge.protectionSpace.host); 
      [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; 
     } else 
      NSLog(@"Not trusting connection to host %@", challenge.protectionSpace.host); 
    } 
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; 
} 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)pResponse { 
    _Authenticated = YES; 
    [connection cancel]; 
    [webvw loadRequest:_FailedRequest]; 
} 
+0

很好的答案。謝謝! – OlivaresF

0

在類中添加這個方法:

-(void) connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{ 
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) 
    { 
     NSURL* baseURL = [NSURL URLWithString:@"yourURL"]; 

     if ([challenge.protectionSpace.host isEqualToString:baseURL.host]) 
     { 
      SecTrustRef trust = challenge.protectionSpace.serverTrust; 

      NSURLCredential *cred = [NSURLCredential credentialForTrust:trust]; 
      [challenge.sender useCredential:cred forAuthenticationChallenge:challenge]; 
     } 
     else 
      NSLog(@"Not trusting connection to host %@", challenge.protectionSpace.host); 
    } 
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; 
}