2016-11-26 79 views
0

我正在嘗試使用Swift 3.0,iOS 10和xCode 8的Alamofire(最新版本)。代碼寫在下面,我不斷收到同樣的問題。請幫助,將在此感謝任何事情。Swift 3.0 Alamofire 4.0 - 域名= NSURLErrorDomain代碼= -999「取消」

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

回答

0

「取消」可如果存在與可能由用戶允許訪問固定一個可疑的HTTPS證書有問題,而且不會發生髮生。

很可能的根本原因是服務器使用的證書不夠完美。

+0

除了告訴服務器修復你的證書問題之外,不應該有辦法解決這個問題。 – ahmad

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 
} 
相關問題