Ios Swift:WebView中加载自签名url时出现TIC SSL信任错误

Ios Swift:WebView中加载自签名url时出现TIC SSL信任错误,ios,webview,shift,self-signed,Ios,Webview,Shift,Self Signed,我试图在iOS应用程序的Webview中加载一个自签名url。其他URL完全加载在我的webview中 我已在info.plist文件中添加,但出现错误:- <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> 您所要做的就是添加一些编码

我试图在iOS应用程序的Webview中加载一个自签名url。其他URL完全加载在我的webview中

我已在info.plist文件中添加,但出现错误:-

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

您所要做的就是添加一些编码内容,而不是在Info.plist中添加key-value

让ServerTrustPolicys:[字符串:ServerTrustPolicy]=[ 您的站点:。禁用评估 ]

在会话管理器中添加上述内容

变量处置:URLSession.AuthChallengeDisposition=.performDefaultHandling

如果您使用alamofire,您可以使用以下方法


我没有使用AlomFire,也不确定在何处以及如何添加ServerTrustPolicys。请帮忙
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()
        }
    }
}
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)
}