当前位置 : 主页 > 手机开发 > 其它 >

swift – Alamofire不处理身份验证挑战

来源:互联网 收集:自由互联 发布时间:2021-06-11
利用Alamofire,我注意到下面的代码没有被断点击中.我建立连接,并收到以下错误:(错误域= NSURLErrorDomain代码= -1200“发生SSL错误,无法建立与服务器的安全连接.”UserInfo = 0x1741b3f60 {_kCFStream
利用Alamofire,我注意到下面的代码没有被断点击中.我建立连接,并收到以下错误:(错误域= NSURLErrorDomain代码= -1200“发生SSL错误,无法建立与服务器的安全连接.”UserInfo = 0x1741b3f60 {_kCFStreamErrorCodeKey = -9806,NSLocalizedRecoverySuggestion =是否要连接到服务器?,NSUnderlyingError = 0x17484b8e0“操作无法完成.(kCFErrorDomainCFNetwork error -1200.)”,NSLocalizedDescription =发生SSL错误,无法建立与服务器的安全连接. ,

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)
    }
}
您无法将值设置为Request Class的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)
}
网友评论