Swift 3.0 Alamofire 4.0 - Domain = NSURLErrorDomain Code = -999“cancelled”

时间:2016-11-26 14:09:33

标签: swift3 alamofire ios10

我正在尝试使用Alamofire(最新版本)与Swift 3.0,iOS 10和xCode 8.目前正在尝试进行一次调用。代码写在下面,我不断遇到同样的问题。请帮助,对此感激不尽。

private class func getDefaultHeaders() -> HTTPHeaders {
        let headers: HTTPHeaders = [
            "Connection" : "keep-alive",
            "token" : "0781d3957fd8da6ee35c4e3124d974a2999925274771234",
            "nonce" : "9b2436331ed908bb2f399568d2adbc4e",
            "uuid" : "uuid",
            "latitude" : "43.656781",
            "longitude" : "-79.380823",
            "Content-Type" : "application/json",
            "userTypeId" : "1",
            "userAgent" : "UserAgent",
            "Content-Length" : "0",
            "Host" : "localhost:8080",
            "User-Agent" : "iOS Example/1.0 (com.alamofire.iOS-Example; build:1; iOS 10.0.0) Alamofire/4.0.0"
        ]
        return headers
    }

func generateCustomSession() -> SessionManager {
    let headers = ServicesController.getDefaultHeaders()
    let configuration = URLSessionConfiguration.default
    configuration.httpAdditionalHeaders = headers
    let manager: SessionManager = SessionManager(configuration: configuration)

    return manager
}

func post(url: String, success:@escaping (JSON) -> Void, failure:@escaping (Error) -> Void) {
        generateCustomSession().request(
            url,
            method: .post,
            encoding: JSONEncoding.default)
            .validate(statusCode: 200..<300)
            .downloadProgress { (progress) -> Void in
                print("download progress: \(progress.fractionCompleted)")
            }
            .responseJSON { (response) -> Void in
                if #available(iOS 10.0, *) {
                    print(response.metrics)
                }

                debugPrint(response)

                if response.result.isSuccess {
                    let resJson = JSON(response.result.value!)
                    success(resJson)
                }
                if response.result.isFailure {
                    let error : Error = response.result.error!
                    failure(error)
                }
        }
    }

2 个答案:

答案 0 :(得分:0)

如果可疑https证书存在问题,可能由用户允许访问而修复,则可能会发生“取消”,但这种情况不会发生。

根本原因很可能是服务器使用的证书不完美。

答案 1 :(得分:0)

我找到了解决问题的方法。以下代码可帮助您取消身份验证质询。我不推荐这个,这对临时解决方案很有用。最好的办法仍然是完全验证身份验证挑战。

    let manager: SessionManager = SessionManager(configuration: configuration, serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies))
    manager.delegate.sessionDidReceiveChallenge = { session, challenge in
            var disposition: URLSession.AuthChallengeDisposition = .cancelAuthenticationChallenge
            var credential: URLCredential?

            if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
                disposition = URLSession.AuthChallengeDisposition.useCredential
                credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
            } else {
                if challenge.previousFailureCount > 0 {
                    disposition = .cancelAuthenticationChallenge
                } else {
                    credential = manager.session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)
                    if credential != nil {
                        disposition = .useCredential
                    }
                }
            }

            return (disposition, credential)
    }


    return manager
}