我试图在 iOS 应用程序的 Webview 中加载一个自签名的 url。其他网址在我的 WebView 中完美加载。
我在我的 info.plist 文件中添加了但出现错误:-
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
这是我的全部错误:-
2019-01-08 12:26:52.386721+0530 Webview demo[761:27005] TIC SSL Trust Error [2:0x282e90480]: 3:0
2019-01-08 12:26:52.405216+0530 Webview demo[761:27005] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9807)
2019-01-08 12:26:52.405283+0530 Webview demo[761:27005] Task .<0> HTTP load failed (error code: -1202 [3:-9807])
2019-01-08 12:26:52.405519+0530 Webview demo[761:27003] NSURLConnection finished with error - code -1202
我是 iOS 新手,请帮助我理解和解决问题。
这是我的 View Controller :-
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://stackoverflow.com")
if let unwrappedUrl = url{
let request = URLRequest(url : unwrappedUrl)
let session = URLSession.shared
let task = session.dataTask(with: request) { (data, response , error) in
if error == nil {
DispatchQueue.main.async {
self.webView.loadRequest(request)
}
}
}
task.resume()
}
}
}
最佳答案
您所要做的就是添加一些编码内容而不是添加键 - Info.plist 中的值
let serverTrustPolicies: [String: ServerTrustPolicy] = [ "your site": .disableEvaluation ]
在您的 session 管理器中添加以上内容。
var 处置:URLSession.AuthChallengeDisposition = .performDefaultHandling
If you use alamofire them you can utilize the below method,
func urlSession(
_ session: URLSession,
task: URLSessionTask,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
{
var disposition: URLSession.AuthChallengeDisposition = .performDefaultHandling
var credential: URLCredential?
if let taskDidReceiveChallenge = taskDidReceiveChallenge {
(disposition, credential) = taskDidReceiveChallenge(session, task, challenge)
} else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
let host = challenge.protectionSpace.host
if
let serverTrustPolicy = session.serverTrustPolicyManager?.serverTrustPolicy(forHost: host),
let serverTrust = challenge.protectionSpace.serverTrust
{
if serverTrustPolicy.evaluate(serverTrust, forHost: host) {
disposition = .useCredential
credential = URLCredential(trust: serverTrust)
} else {
disposition = .cancelAuthenticationChallenge
}
}
} else {
if challenge.previousFailureCount > 0 {
disposition = .rejectProtectionSpace
} else {
credential = self.credential ?? session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)
if credential != nil {
disposition = .useCredential
}
}
}
completionHandler(disposition, credential)
}
关于ios - swift : TIC SSL Trust Error on loading self signed url in WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54086698/