Alamofire 不处理身份验证质询

2024-04-04

使用 Alamofire,我注意到下面的代码没有遇到断点。我建立连接,并收到以下错误:(Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo=0x1741b3f60 {_kCFStreamErrorCodeKey=-9806, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, NSUnderlyingError=0x17484b8e0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1200.)", NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made.,

func connection(urlRequest:NSURLRequest,rest:RESTFull?, completion: (AnyObject?, NSError?)->Void){
    let req = request(urlRequest).responseJSON(options: .AllowFragments) { (_, response, data, error) -> Void in
        if let actualData: AnyObject = data {
            completion(actualData, nil)
        }else {
            completion(nil, error)
        }
    }

    req.delegate.taskDidReceiveChallenge = { session,_, challenge in
        println("Got challenge: \(challenge), in session \(session)")

        var disposition: NSURLSessionAuthChallengeDisposition = .UseCredential
        var credential: NSURLCredential = NSURLCredential()

        if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust){
            disposition = NSURLSessionAuthChallengeDisposition.UseCredential
            credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
        }
        return(disposition, credential)
    }
}

您无法为请求类的taskDidReceiveChallenge 设置值。您可以使用 Manager 类的委托来代替。

Manager.sharedInstance.delegate.taskDidReceiveChallenge = { session, _, challenge in
    print("Got challenge: \(challenge), in session \(session)")
    var disposition: NSURLSessionAuthChallengeDisposition = .UseCredential
    var credential: NSURLCredential = NSURLCredential()

    if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust){
        disposition = NSURLSessionAuthChallengeDisposition.UseCredential
        credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
    }
    return(disposition, credential)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Alamofire 不处理身份验证质询 的相关文章

随机推荐