I'm trying to code a basic example of a REST client using Alamofire 4 and swift 3, but whatever the service I try to access, the response is allways nil.
A'm I missing any configuration?
This is my code:
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request("https://httpbin.org/get").responseJSON { response in
print(response.request) // original URL request
print(response.response) // HTTP URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
}
Podfile
platform :ios, '9.0'
target 'RESTClient' do
use_frameworks!
# Pods for RESTClient
pod 'Alamofire', '~> 4.2'
pod 'SwiftyJSON'
end
Info.plist
As long as Alamofire uses asynchronous requests, the next part of the code was being executed before the requisition.
So, to assure the order of execution, the code that depends of the response must be into the closure.