Alamofire not handling Authentication challenge

回眸只為那壹抹淺笑 提交于 2019-12-22 09:03:13

问题


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)
    }
}

回答1:


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)
}


来源:https://stackoverflow.com/questions/31279362/alamofire-not-handling-authentication-challenge

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!