ATS設(shè)置
按照慣例寫一個UIWebView虫给,用來加載網(wǎng)頁:
_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一下看看加載出來了嗎里烦?如果發(fā)現(xiàn)屏幕一片白并沒有出現(xiàn)網(wǎng)頁內(nèi)容加袋,不要驚訝速侈,看看你的控制臺是不是報出以下錯誤:
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
這個怎么解決呢?沒錯ATS設(shè)置:去plist文件里添加一項(xiàng)App Transport Security Settings剪返,它是個字典類型废累,給它增加一對鍵值,鍵:Allow Arbitrary Loads 脱盲,值設(shè)為YES邑滨。
以上,搞定ATS設(shè)置钱反,網(wǎng)頁成功加載了:
如果你的網(wǎng)頁是self signed website掖看,那么你的屏幕應(yīng)該還是一片白,并且控制臺又報錯:
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
注意:這兩次錯誤并不是一樣的,后面數(shù)字代碼不同
(并不太清楚這個碼代表的意思面哥,有知道的朋友請留言告知哎壳,感謝。)
NSURLConnection
使用webview加載自簽名https站點(diǎn)的時候尚卫,必須在請求的時候?qū)⒃撜军c(diǎn)設(shè)置為安全的归榕,才能繼續(xù)訪問。所以我們需要在webview開始加載網(wǎng)頁的時候首先判斷判斷該站點(diǎn)是不是https站點(diǎn)焕毫,如果是的話蹲坷,先讓他暫停加載,用NSURLConnection 來訪問改站點(diǎn)邑飒,然后再身份驗(yàn)證的時候循签,將該站點(diǎn)置為可信任站點(diǎn)。然后在用webview重新加載請求疙咸。
直接上代碼:
#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];
}
以上設(shè)置县匠,可成功加載自簽名網(wǎng)頁。
但是,雖然加載成功乞旦,但是控制臺還是報了以下錯誤:
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9843)
最后
推薦參考鏈接:
stackoverflow Q1
stackoverflow Q2