Alamofire不处理身份验证挑战 [英] Alamofire not handling Authentication challenge
问题描述
利用Alamofire,我注意到下面的代码没有被断点击中。我建立了连接,并收到以下错误:(错误域= NSURLErrorDomain代码= -1200发生SSL错误,无法建立与服务器的安全连接。 UserInfo = 0x1741b3f60 {_kCFStreamErrorCodeKey = -9806,NSLocalizedRecoverySuggestion =是否仍要连接到服务器?,NSUnderlyingError = 0x17484b8e0操作无法完成。(kCFErrorDomainCFNetwork错误-1200。),NSLocalizedDescription =已发生SSL错误,并且已安全连接无法建立服务器。
Utilizing Alamofire, Im noticing that the code below isn't being hit with a breakpoint. I make a connection, and I get the following error: (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类的委托。
You can't set value to the Request Class's taskDidReceiveChallenge. You can use Manager class's delegate instead.
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)
}
这篇关于Alamofire不处理身份验证挑战的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!