2017-07-11 22 views

答えて

0

AppDelegateのapplication(_:didFinishLaunchingWithOptions:)メソッドでこの関数を呼び出します。

func configureAlamofire() { 
    Alamofire.SessionManager.default.session.configuration.timeoutIntervalForRequest = 15 // in seconds 
    Alamofire.SessionManager.default.session.configuration.timeoutIntervalForResource = 15 // in seconds 
} 
0
let manager = Alamofire.SessionManager.default 
manager.session.configuration.timeoutIntervalForRequest = 120 

manager.request("yourUrl", method: .post, parameters: ["parameterKey": "value"]) 
     .responseJSON { 
      response in 
      switch (response.result) { 
      case .success: 
       //do json stuff 
       break 
      case .failure(let error): 
       if error._code == NSURLErrorTimedOut { 
        //timeout here 
       } 
       print("\n\nAuth request failed with error:\n \(error)") 
       break 
      } 
     } 
2

カスタムタイムアウトをしたい場合は、いずれかのご要望に値を設定したり、カスタム設定でカスタムURLSessionを作成:alamofire documentation

let configuration = URLSessionConfiguration.default 
configuration.timeoutIntervalForRequest = 10 // seconds 
configuration.timeoutIntervalForResource = 10 
let sessionManager = Alamofire.SessionManager(configuration: configuration) 

// OR

var request = URLRequest(url: URL(string: "myApi.com/Api/login")!, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 20) 
request.httpMethod = "GET" 
request.setValue("application/json", forHTTPHeaderField: "Content-Type") 

Alamofire.request("https://httpbin.org/get").responseJSON { response in 
    print(response) 
    if let json = response.result.value { 
     print("JSON: \(json)") 
    } 
} 
関連する問題