ios - swift : TIC SSL Trust Error on loading self signed url in WebView

标签 ios webview shift self-signed

我试图在 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/

相关文章:

c - 在 C 中移动单词时跳过标点符号

ios - 从 Parse 中删除推送通知历史条目

objective-c - OSX - 在 WebView 中加载图像

ios - iPhone 和 Apple Watch 应用程序之间的核心数据共享

webview - 是否可以在 WinRT Web View 中更改 document.location.protocol 值?

javascript - Javascript 执行启动 chrome 而不是 webview

javascript - 为什么 shift() 在使用 apply() 动态调用的函数中对我的参数工作不正常?

bit - 逆向工程,左移七位

ios - 我怎样才能让 UIPickerview 自动更新 UILabel

iphone - Objective-C : Proper way to init an NSArray that is a @property