Code like this:
import Foundation
import Alamofire
struct Request {
var alamoFireManager : SessionManager?
init() {
let configuration = URLSessionConfi
SessionManager
not works for me correctly. It's better to using code like this in Alamofire:
let request = URLRequest(url: url)
request.httpMethod = "GET"
//Some configuration
Alamofire.request(request).responseJSON {
response in
}
I have done this and it's working (swift 4 Code).
import UIKit
import Alamofire
class SplashViewController: UIViewController {
var alamoFireManager = Alamofire.SessionManager.default
override func viewDidLoad() {
super.viewDidLoad()
self.callPostApi()
}
func callPostApi() {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 20 // seconds
configuration.timeoutIntervalForResource = 20
configuration.requestCachePolicy = .useProtocolCachePolicy
alamoFireManager = Alamofire.SessionManager(configuration: configuration)
alamoFireManager.request(url, method: .post, parameters: ["jsondata":base64EncodedString])
.responseJSON { response in
switch (response.result) {
case .success:
print(response.data as? Data)
if let json = response.result.value {
print("JSON: \(json)") // Here is your JSON Response
}
//do json stuff
case .failure(let error):
if error._code == NSURLErrorTimedOut || error._code == -1005{
//HANDLE TIMEOUT HERE
print("TimeOut")
}
print("\n\nAuth request failed with error:\n \(error)")
break
}
}
}
}
For me was a timeout error. One option is increase the value of the timeout (bad option), the better is improve the performance of your API/Application.