標籤:https uiwebview
今天在載入https網站的時候遇到如下的錯誤問題。所以對自己之前寫的iOS內嵌webview做了一些修改,可以讓它載入http網站也可以讓它載入https網站、
下面是我載入https網站的時候出現的錯誤。
error:
NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
HTTPS 超文本傳輸安全性通訊協定(縮寫:HTTPS,英語:Hypertext Transfer Protocol Secure)是超文字傳輸通訊協定 (HTTP)和SSL/TLS的組合,
HTTPS的主要思想是在不安全的網路上建立一安全通道,並可在使用適當的加密包和伺服器憑證可被驗證且可被信任時,對竊聽和中間人攻擊提供合理的保護。
HTTPS的信任繼承基於預先安裝在瀏覽器中的憑證授權單位(如VeriSign、Microsoft等)(意即“我信任憑證授權單位告訴我應該信任的”)。因此,一個到某網站的HTTPS串連可被信任,如果伺服器搭建自己的https 也就是說採用自認證的方式來建立https通道,這樣一般在用戶端是不被信任的,所以我們一般在瀏覽器訪問一些https網站的時候會有一個提示,問你是否繼續。
使用webview載入https網站的時候,也會出現這樣的情況,也就是說我們必須在請求的時候將該網站設定為安全的,才能繼續訪問
所以我們需要在webview開始載入網頁的時候首先判斷判斷該網站是不是https網站,如果是的話,先然他暫停載入,先用
NSURLConnection 來訪問改網站,然後再身分識別驗證的時候,將該網站置為可信任網站。然後在用webview重新載入請求。
#pragma mark - UIWebViewDelegate- (BOOL)webView:(UIWebView *)awebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ NSString* scheme = [[request URL] scheme]; NSLog(@"scheme = %@",scheme); //判斷是不是https if ([scheme isEqualToString:HTTPS]) { //如果是https:的話,那麼就用NSURLConnection來重發請求。從而在請求的過程當中吧要請求的URL做信任處理。 if (!self.isAuthed) { originRequest = request; NSURLConnection* conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; [conn start]; [awebView stopLoading]; return NO; } } [self reflashButtonState]; [self freshLoadingView:YES]; NSURL *theUrl = [request URL]; self.currenURL = theUrl; return YES;}
在NSURLConnection 代理方法中處理信任問題。
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ if ([challenge previousFailureCount]== 0) { _authed = YES; //NSURLCredential 這個類是表示身分識別驗證憑據不可變對象。憑證的實際型別宣告的類的建構函式來確定。 NSURLCredential* cre = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; [challenge.sender useCredential:cre forAuthenticationChallenge:challenge]; } else<pre name="code" class="objc">
最後在NSURLConnection 代理方法中收到響應之後,再次使用web view載入https網站。
pragma mark ================= NSURLConnectionDataDelegate <NSURLConnectionDelegate>- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response{ NSLog(@"%@",request); return request; }- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ self.authed = YES; //webview 重新載入請求。 [webView loadRequest:originRequest]; [connection cancel];}
推薦兩個stackoverflow地址:
http://stackoverflow.com/questions/11573164/uiwebview-to-view-self-signed-websites-no-private-api-not-nsurlconnection-i
http://stackoverflow.com/questions/20365774/call-https-url-in-uiwebview