WebView loadRequest請求錯誤"NSURLConnection finished with error - code -1022"

執行下面代碼web

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.bing.com"]]];

webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error打印出來的的錯誤以下ui

1 [6587:75627] NSURLConnection finished with error - code -1022
2 [6587:75574] Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy 
3 requires the use of a secure connection." UserInfo={NSUnderlyingError=0x600000641f50 
4 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "The resource could not be loaded because the App Transport Security policy requires 
5 the use of a secure connection." UserInfo={NSErrorFailingURLStringKey=http://www.bing.com/, 
6 NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.,
7  NSErrorFailingURLKey=http://www.bing.com/}}, NSErrorFailingURLStringKey=http://www.bing.com/, NSErrorFailingURLKey=http://www.bing.com/, 
8 NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.}

 

解決方案很簡單:spa

URLString必須使用https的請求webview就能正常顯示了code

 

但後來發現要加載http://localhost:8080而後悲劇了,看來仍是要研究一下具體機制了,在XCode 9就會出現錯誤中提示了很明顯的信息blog

The resource could not be loaded because the App Transport Security policy 

去百度一下ATS policy,就能夠知道很詳細的答案了,在SupportFiles->info.plist文件裏面添加下面屬性就行了,有直接的選項,不須要本身填寫ip

done!webview

相關文章
相關標籤/搜索