The certificate for this server is invalid

前端 未结 9 1831
花落未央
花落未央 2021-02-13 18:04

I know that if I use following nsurlconnectiondelegate it will be fixed

– connection:willSendRequestForAuthenticationChallenge: – connection:canAuthenti

9条回答
  •  隐瞒了意图╮
    2021-02-13 18:55

    Try this.

    Initiate your session using custom session config as shown below:

    let session = URLSession(configuration: URLSessionConfiguration.ephemeral,
                                     delegate: self,
                                     delegateQueue: nil)
    

    Implement the following delegate callback method:

    public func urlSession(_: URLSession, task _: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        guard let serverTrust = challenge.protectionSpace.serverTrust else {
            return completionHandler(URLSession.AuthChallengeDisposition.useCredential, nil)
        }
        return completionHandler(URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
    }
    

提交回复
热议问题