2020-08-20

ATS設(shè)置

按照慣例寫(xiě)一個(gè)UIWebView,用來(lái)加載網(wǎng)頁(yè):

_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一下看看加載出來(lái)了嗎?如果發(fā)現(xiàn)屏幕一片白并沒(méi)有出現(xiàn)網(wǎng)頁(yè)內(nèi)容,不要驚訝,看看你的控制臺(tái)是不是報(bào)出以下錯(cuò)誤:

NSURLSession/NSURLConnection HTTP load failed(kCFStreamErrorDomainSSL,-9802)

這個(gè)怎么解決呢?沒(méi)錯(cuò)ATS設(shè)置:去plist文件里添加一項(xiàng)App Transport Security Settings,它是個(gè)字典類型,給它增加一對(duì)鍵值,鍵:Allow Arbitrary Loads ,值設(shè)為YES。

以上,搞定ATS設(shè)置,網(wǎng)頁(yè)成功加載了:

Simulator Screen Shot.png

如果你的網(wǎng)頁(yè)是self signed website,那么你的屏幕應(yīng)該還是一片白,并且控制臺(tái)又報(bào)錯(cuò):

NSURLSession/NSURLConnection HTTP load failed(kCFStreamErrorDomainSSL,-9813)

注意:這兩次錯(cuò)誤并不是一樣的,后面數(shù)字代碼不同

(并不太清楚這個(gè)碼代表的意思,有知道的朋友請(qǐng)留言告知,感謝。)

NSURLConnection

使用webview加載自簽名https站點(diǎn)的時(shí)候,必須在請(qǐng)求的時(shí)候?qū)⒃撜军c(diǎn)設(shè)置為安全的,才能繼續(xù)訪問(wèn)。所以我們需要在webview開(kāi)始加載網(wǎng)頁(yè)的時(shí)候首先判斷判斷該站點(diǎn)是不是https站點(diǎn),如果是的話,先讓他暫停加載,用NSURLConnection 來(lái)訪問(wèn)改站點(diǎn),然后再身份驗(yàn)證的時(shí)候,將該站點(diǎn)置為可信任站點(diǎn)。然后在用webview重新加載請(qǐng)求。

直接上代碼:

#pragmamark - 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];returnNO;}returnYES;}#pragmamark - 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];}#pragmamark - 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)頁(yè)。

但是,雖然加載成功,但是控制臺(tái)還是報(bào)了以下錯(cuò)誤:

NSURLSession/NSURLConnection HTTP load failed(kCFStreamErrorDomainSSL,-9843)

作者:啃手高手

鏈接:http://www.itdecent.cn/p/91eb1d8817f2

來(lái)源:簡(jiǎn)書(shū)

著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容