Alamofire - error code -999
Here's what it should look like:
static let alamofireManager: SessionManager = {
let sessionConfiguration = URLSessionConfiguration.default
sessionConfiguration.timeoutIntervalForRequest = 10
return Alamofire.SessionManager(configuration: sessionConfiguration)
}()
Getting an error when calling the Alamofire in a class function
You need to add Do-Catch Statement
catch – If the throwing method fails and raises an error, the execution will fall into this catch block.
class NetworkLayer{
class func requested(_ request:Router,completion:@escaping(Result<Data,Error>) -> Void){
ProgressHUD.show() //if response comes that loader run
AF.request(request).responseJSON{ (response) in
switch response.result{
case .success(let data):
do{
let getDishesData = data as? [String:Any]
let resp = try JSONSerialization.data(withJSONObject: getDishesData?["data"], options: .prettyPrinted)
completion(.success(response))
}catch{
print(error)
completion(.failure(error))
}
case .failure(let error):
completion(.failure(error))
}
}
}
}
One more suggestion for you here no need to do JSONSerialization because responseJSON gives you direct response(That Alamofire will do JSONSerialization).
Final code
class NetworkLayer{
class func requested(_ request:Router,completion:@escaping(Result<Data,Error>) -> Void){
ProgressHUD.show() //if response comes that loader run
AF.request(request).responseJSON{ (response) in
switch response.result{
case .success(let response):
do{
print(response)
completion(.success(response))
}catch{
print(error)
completion(.failure(error))
}
case .failure(let error):
completion(.failure(error))
}
}
}
}
Alamofire returns .Success on error HTTP status codes
From the Alamofire manual:
Validation
By default, Alamofire treats any completed request to be successful,
regardless of the content of the response. Calling validate before a
response handler causes an error to be generated if the response had
an unacceptable status code or MIME type.
You can manually validate the status code using the validate
method, again, from the manual:
Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.response { response in
print(response)
}
Or you can semi-automatically validate the status code and content-type using the validate
with no arguments:
Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
.validate()
.responseJSON { response in
switch response.result {
case .success:
print("Validation Successful")
case .failure(let error):
print(error)
}
}
Alamofire error is always nil, while request fails
Need to add .validate() because of by default, Alamofire treats any completed request to be successful, regardless of the content of the response.
From Alamofire documentation:
Validation
By default, Alamofire treats any completed request to be successful,
regardless of the content of the response. Calling validate before a
response handler causes an error to be generated if the response had
an unacceptable status code or MIME type.Manual Validation
Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.response { _, _, _, error in
println(error)
}
Automatic Validation
Automatically validates status code within 200...299 range, and that
the Content-Type header of the response matches the Accept header of
the request, if one is provided.Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
.validate()
.response { _, _, _, error in
println(error)
}
Related Topics
Receive Accelerometer Updates in Background Using Coremotion Framework
How to Make Offline Database for My App
Setting Up a Parent-Child Relationship in Core Data
How to Determine Which Textfield Is Active Swift
Present Uialertcontroller from Appdelegate
How to Pass Information Between Storyboard Segues
Invalid Signature (Invalid Binary) on Itunes Connect
Uiimagepicker Cameraoverlayview Appears on Retake Screen
Differentiate Between Screen Lock and Home Button Press on iOS7
How to Set Http Header Fields in Objective-C
Swift iOS9 New Contacts Framework - How to Retrieve Only Cncontact That Has a Valid Email Address
How to Identify iOS Device Uniquely
How to Detect iOS 6 and All Minor Versions by User Agent
Getting Data from Each Uitableview Cells Swift
Add Rounded Corners to All Uiimageviews
Use Different Googleservice-Info.Plist for Different Build Schemes