加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 04:26:52 所属栏目:百科 来源:网络整理
导读:利用Alamofire,我注意到下面的代码没有被断点击中.我建立连接,并收到以下错误:(错误域= NSURLErrorDomain代码= -1200“发生SSL错误,无法建立与服务器的安全连接.”UserInfo = 0x1741b3f60 {_kCFStreamErrorCodeKey = -9806,NSLocalizedRecoverySuggestion =
利用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)
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读