#ATS設置 按照慣例寫一個UIWebView,用來加載網頁:git
_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
_webView.delegate = self;
[self.view addSubview:_webView];
NSURL *url = [NSURL URLWithString:@"https://github.com/"];
_request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:_request];
複製代碼
run一下看看加載出來了嗎?若是發現屏幕一片白並無出現網頁內容,不要驚訝,看看你的控制檯是否是報出如下錯誤:github
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
複製代碼
這個怎麼解決呢?沒錯ATS設置:去plist文件裏添加一項App Transport Security Settings,它是個字典類型,給它增長一對鍵值,鍵:Allow Arbitrary Loads ,值設爲YES。web
以上,搞定ATS設置,網頁成功加載了:api
若是你的網頁是self signed website,那麼你的屏幕應該仍是一片白,而且控制檯又報錯:安全
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
複製代碼
注意:這兩次錯誤並非同樣的,後面數字代碼不一樣 (並不太清楚這個碼錶明的意思,有知道的朋友請留言告知,感謝。)bash
##NSURLConnectionui
使用webview加載自簽名https站點的時候,必須在請求的時候將該站點設置爲安全的,才能繼續訪問。因此咱們須要在webview開始加載網頁的時候首先判斷判斷該站點是否是https站點,若是是的話,先讓他暫停加載,用NSURLConnection 來訪問改站點,而後再身份驗證的時候,將該站點置爲可信任站點。而後在用webview從新加載請求。this
直接上代碼:url
#pragma mark - UIWebViewDelegate
// Note: This method is particularly important. As the server is using a self signed certificate,
// we cannot use just UIWebView - as it doesn't allow for using self-certs. Instead, we stop the // request in this method below, create an NSURLConnection (which can allow self-certs via the delegate methods // which UIWebView does not have), authenticate using NSURLConnection, then use another UIWebView to complete // the loading and viewing of the page. See connection:didReceiveAuthenticationChallenge to see how this works. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; { NSLog(@"Did start loading: %@ auth:%d", [[request URL] absoluteString], _authenticated); if (!_authenticated) { _authenticated = NO; _urlConnection = [[NSURLConnection alloc] initWithRequest:_request delegate:self]; [_urlConnection start]; return NO; } return YES; } #pragma mark - NURLConnection delegate - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge; { NSLog(@"WebController Got auth challange via NSURLConnection"); if ([challenge previousFailureCount] == 0) { _authenticated = YES; NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; [challenge.sender useCredential:credential forAuthenticationChallenge:challenge]; } else { [[challenge sender] cancelAuthenticationChallenge:challenge]; } } // We use this method is to accept an untrusted site which unfortunately we need to do, as our PVM servers are self signed. - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; } #pragma mark - NSURLConnectionDataDelegate - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response; { NSLog(@"WebController received response via NSURLConnection"); // remake a webview call now that authentication has passed ok. _authenticated = YES; [_web loadRequest:_request]; // Cancel the URL connection otherwise we double up (webview + url connection, same url = no good!) [_urlConnection cancel]; } 複製代碼
以上設置,可成功加載自簽名網頁。spa
可是,雖然加載成功,可是控制檯仍是報瞭如下錯誤:
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9843)
複製代碼
##最後 推薦參考連接: stackoverflow Q1 stackoverflow Q2