I am using the version 4.3.0 of Alamofire and executing on 10.1 . If I build a custom SessionManager for set a timeout, Alamofire throws a NSURLErrorDomain
I asked in StackOverflow but every solution does not work.
import Foundation
import Alamofire
class NetworkManager {
var manager: SessionManager!
init() {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 10
configuration.timeoutIntervalForResource = 10
configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders
manager = Alamofire.SessionManager(configuration: configuration)
}
}
In my ViewController:
let parameters: Parameters = ["email":email.text!]
let alamofireManager = NetworkManager.init()
alamofireManager.manager.request("http://s574275930.mialojamiento.es/sedup/api/cliente/activar", method: .post, parameters: parameters).responseJSON { response in
switch (response.result) {
case .success:
// OK
break
case .failure(let error):
// <---- Here is where is called when NSURLErrorDomain is thrown
break
}
}
I have the same problem. All StackOverflow articles nonworking. Please update this ticket if you find any solution...
Hello I solved the problem with:
var request = URLRequest(url: NSURL.init(string: "YOUR_URL") as! URL)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.timeoutInterval = 10 // 10 secs
let postString = "param1=\(var1)¶m2=\(var2)"
request.httpBody = postString.data(using: .utf8)
Alamofire.request(request).responseJSON {
response in
}
I pupdated my question in StackOverflow
@tato469 It works. Thank you.
@tato469 it works, thank you
tato469's solution was the only one working for me
This solution provided by @tato469 was not working for me, as I get Could not cast value of type 'NSMutableURLRequest' to 'Alamofire.URLRequestConvertible' error message.
@gabrieloliva did you finally solve it?
@tato469 I find out that timeout I was getting wasn鈥檛 because of Alamofire. I was using a dependency called Timberjack for networking logs in console. This dependency setup was putting something in header that my backend could not handler.
This solution didn't work for me on when using VPN
I think this issue has to do with the SessionManager getting deallocated and cancelling the request.
See https://github.com/Alamofire/Alamofire/issues/1684 and https://github.com/Alamofire/Alamofire/issues/157
var request = URLRequest(url: NSURL.init(string: "YOUR_URL") as! URL)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.timeoutInterval = 10 // 10 secs
let postString = "param1=(var1)¶m2=(var2)"
request.httpBody = postString.data(using: .utf8)
Alamofire.request(request).responseJSON {
response in
print(request.description)
}
Thanks you @tato469
Api is getting failed on iOS 13.3 but is working fine on below iOS 13.3.
finished with error [-1001]
Error Domain=NSURLErrorDomain Code=-1001 "The request timed out."
UserInfo={_kCFStreamErrorCodeKey=-2102, NSUnderlyingError=0x60000193b060
{Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102,
_kCFStreamErrorDomainKey=4}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <8F950888-C8E6-47B6-9FD8-78719B4FD231>.<8>,
_NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <8F950888-C8E6-47B6-9FD8-78719B4FD231>.<8>"
),
NSLocalizedDescription=The request timed out.,
NSErrorFailingURLStringKey=https://api.url.com/oauth,
NSErrorFailingURLKey=https://api.url.com/oauth, _kCFStreamErrorDomainKey=4}
func commonNetworkCallWithHeader(header :[String:String],url:String,method:HTTPMethod,parameters : [String:Any]?,completionHandler:@escaping (JSON?,String?)->Void) {
let configuration = URLSessionConfiguration.background(withIdentifier: "")
let manager = Alamofire.SessionManager(configuration: configuration)
manager.session.configuration.timeoutIntervalForRequest = 60
manager.startRequestsImmediately = true
Alamofire.request(url, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: header).responseJSON { (response) in
if(response.result.isSuccess){
if let data = response.result.value{
let json = JSON(data)
completionHandler(json,nil)
return
}
}
completionHandler(nil,response.result.error?.localizedDescription)
}
}
https://stackoverflow.com/questions/59559019/alamofire-api-request-timeout-only-on-ios-13-3-devices
For Swift 3.x / Swift 4.0 / Swift 5.0 users with Alamofire >= 5.0
Used request modifier to increase and decrease the timeout interval.
Alamofire's request creation methods offer the most common parameters for customization but sometimes those just aren't enough. The URLRequests created from the passed values can be modified by using a RequestModifier closure when creating requests. For example, to set the URLRequest's timeoutInterval to 120 seconds, modify the request in the closure.
var manager = Session.default
manager.request(urlString, method: method, parameters: dict, headers: headers, requestModifier: { $0.timeoutInterval = 120 }).validate().responseJSON { response in
}
OR
RequestModifiers also work with trailing closure syntax.
var manager = Session.default
manager.request("https://httpbin.org/get") { urlRequest in
urlRequest.timeoutInterval = 60
urlRequest.allowsConstrainedNetworkAccess = false
}
.response(...)
Most helpful comment
Hello I solved the problem with:
I pupdated my question in StackOverflow