【问题标题】:Allow Arbitrary Loads not working after Swift 5 conversionSwift 5 转换后允许任意负载不起作用
【发布时间】:2019-10-20 03:23:43
【问题描述】:

我有一个应用程序,它使用自签名证书与本地托管服务器上的 API 进行交互。我在 info.plist 中包含了允许任意加载的代码,还包含了当我们收到挑战时的 URLSessionDelegate。

我在 urlsession 函数中有一个打印行,并且该行永远不会被打印,所以我觉得该代码永远不会被调用。我在我的班级中继承了 URLSessionDelegate。这一切都适用于 Swift 4。

func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {

    //accept all certs when testing, perform default handling otherwise
        print("Accepting cert as always")
        completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
}

这是收到的错误:

2019-06-04 09:59:55.604023-0500 The MUT[8866:1617557] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9843)
2019-06-04 09:59:55.604056-0500 The MUT[8866:1617557] Task <F8D75C99-49ED-45BE-B764-942B748942DB>.<1> HTTP load failed (error code: -1202 [3:-9843])
2019-06-04 09:59:55.604150-0500 The MUT[8866:1617558] Task <F8D75C99-49ED-45BE-B764-942B748942DB>.<1> finished with error - code: -1202```

【问题讨论】:

    标签: swift nsurlsession app-transport-security swift5


    【解决方案1】:

    这个问题已经解决了。我在会话声明中添加了 delegate: self, delegateQueue: myOpQueue。

    let myOpQueue = OperationQueue()
    ...
    let session = Foundation.URLSession(configuration: configuration, delegate: self, delegateQueue: myOpQueue)
    

    【讨论】:

      【解决方案2】:

      确保您已正确签署您的委托或尝试在您的 ViewController 的 viewDidLoad 中签署它。

      这个错误:HTTP Load Failed 是这样解决的:

      NSAppTransportSecurity -> NSAllowsArbitraryLoads = true

      【讨论】:

        猜你喜欢
        • 2016-03-06
        • 1970-01-01
        • 2017-03-23
        • 2017-02-13
        • 2022-01-26
        • 1970-01-01
        • 2017-04-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多