I am using AFNetworking with Swift 3.0 and I am stuck on one code.
func getJSON()
{
let manager = AFHTTPSessionManager()
manager.get(
url,
parameters: nil,
success:
{
(operation: URLSessionTask!, responseObject: Any?) in
print("JSON: " + responseObject!.description)
self.matchesArray = responseObject!.object(forKey: "matches")! as? NSMutableArray
self.tollBothPlazaTableView.reloadData()
},
failure:
{
(operation: URLSessionTask!, error: NSError) in
print("Error: " + error.localizedDescription)
}
)
}
It shows error on failure block.
Cannot convert value of type '(URLSessionTask!, NSError) -> ()' to expected argument type '((URLSessionDataTask?, Error) -> Void)?'`
Can someone explain what is wrong in my code. Also the correct way to use closures? (I am new to swift).
Error is clearly saying that use Error instead of NSError, in Swift 3 you need to use Error instead of NSError. So change your code like below.
func getJSON() {
let manager = AFHTTPSessionManager()
manager.get(
url,
parameters: nil,
success:
{
(operation, responseObject) in
if let dic = responseObject as? [String: Any], let matches = dic["matches"] as? [[String: Any]] {
print(matches)
}
DispatchQueue.main.async {
self.tollBothPlazaTableView.reloadData()
}
},
failure:
{
(operation, error) in
print("Error: " + error.localizedDescription)
})
}
Note: Always perform UI changes on main thread when you are on background thread, so batter to reload your tableView on main thread like I have done, Also use Swift native Array and Dictionary instead of NSArray and NSDictionary.
**Its Better to use Alamofire(same developer) in swift 3 **
func jsonRequest()
{
let url = "url"
//if you want to add paramter
parametr = ["username" : "user" , "password"]
Alamofire.request(url, method: .post, parameters: nil, encoding: JSONEncoding.default)
.responseJSON { response in
// print(response)
//to get status code
if let status = response.response?.statusCode {
switch(status){
case 201:
print("example success")
default:
print("error with response status: \(status)")
}
}
//to get JSON return value
if let array = response.result.value as? //NSDictionary [[String : Any]]
{
}
}
}
Related
I am calling normal API call with Alamofire, And I am not passing any data with that in networking manager class.
My Networking class is
func executeGetRequest(url:String,requestVC:UIViewController,completionHandler:#escaping (_ responseObject:Any?) -> Void!,failureHandler:#escaping (_ connectionError:NSError?) -> Void!){
//Checking internet alert
if !self.isConnectedToInternet(){
// requestVC.showAlert(kText_AppName, message: kText_NoInternet)
return
}
requestVC.showLoader()
Alamofire.request(url).responseJSON {
(response:DataResponse) in
requestVC.removeLoader()
switch(response.result) {
case .success(_):
if response.result.value != nil{
completionHandler (response.result.value)
}
break
case .failure(let error):
failureHandler (error as NSError?)
break
}
}
}
and i am calling it from my main class
kNetworkManager.executeGetRequest(url: kAppAccessTokenURL, requestVC: self, completionHandler: {
(responseObject) -> () in
print("responseObject:\(responseObject!)")
}, failureHandler: {(error)-> () in
print("response object:\(error!)")
self.showAlert(message: (error?.description)!, title: kText_AppName)
if error?._code == NSURLErrorTimedOut {
//timeout here
self.showAlert(message: kText_timeout, title: kText_AppName)
}
})
Its getting always request fail and showing error as responseSerializationFailed
if I call directly in Main Class without manager class like
Alamofire.request(kAppAccessTokenURL).responseString { response in
I am able to getting response, can anyone suggest me where getting wrong in Network class.
Here you
Alamofire.request(kAppAccessTokenURL).responseString
and there
Alamofire.request(url).responseJSON
look to that
let jsonText = "{\"first_name\":\"Sergey\"}"
var dictonary:NSDictionary?
if let data = jsonText.dataUsingEncoding(NSUTF8StringEncoding) {
do {
dictonary = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String:AnyObject]
if let myDictionary = dictonary
{
print(" First name is: \(myDictionary["first_name"]!)")
}
} catch let error as NSError {
print(error)
}
}
I'm trying to make a Get request with Alamofire for swift. When I run in Simulator or device with iOS 10 it works fine. When I run my app on Devices with iOS 9.x or prior, I'm getting "cannot parse response". The JSON response is right. I checked in postman.
There's my code:
lass func getStores(latitude:Float, longitude: Float, completion : #escaping (Array<Store>?, NSError?) -> ()) {
let latString: String = "\(latitude)"
let lonString: String = "\(longitude)"
var listStores: Array<Store> = []
let urlFull : String = ConstantHelper.kUrlStore
Alamofire.request(urlFull, method: .get, parameters: ["latitude":latString, "longitude": lonString], encoding: JSONEncoding(options: []), headers: [:]).validate()
.responseJSON { response in
switch response.result {
case .success:
if let repoJSON = response.result.value {
print(repoJSON)
let jsonArray = repoJSON as! NSArray
for item in jsonArray {
guard let store = Store(json: item as! JSON) else
{
print("Issue deserializing model")
return
}
listStores.append(store)
}
completion(listStores, nil)
}
break
case .failure(let error):
completion(nil, error as NSError?)
break
}
}
}
}
I solved my problem with the following code without JSON enconding in request:
Alamofire.request(urlFull, method: .get, parameters: ["latitude":latString, "longitude": lonString]).validate()
I don't know why works in Swift 2.x and in Swift 3.0 with iOS 10 with encoding and don't in ios 9...but the code above works in all situations. And its really no needed to enconding this request
I need to wait for response.response?.allHeaderFields data before executing function. I've searched the net and didn't quite get how to add "completion handler" to alamofire request. Or if there are any other ways to make the function wait.
#IBAction func comfirmation(sender: UIButton) {
if CodeTextField.text != "" {
print("in comfirmation function")
let comfirmationRequestData = [
"phone" : "\(appDelegate.savedNumber)",
"code" : "\(CodeTextField.text!)"
]
Alamofire.request(.POST,
"http://192.168.214.241:4000/login",
parameters: comfirmationRequestData,
encoding: .JSON).responseJSON {
response in
switch response.result {
case .Success:
let jsonDecrypted = JSON(response.result.value!)
print(jsonDecrypted)
let headerFile = response.response?.allHeaderFields as? [String:String]
print(headerFile)
case .Failure(let error):
print(error)
}
}
print("in comfirmation function. success")
appDelegate.defaults.setValue(appDelegate.savedNumber, forKey: "phoneNumber")
} else {
print("in comfirmation function. failed")
}
}
Use Alamofire like this
func postRequest( urlSuffix : String, params:[String : AnyObject]?, filterParams : [String]?, success: (response: AnyObject!) -> Void, failure: (error: NSError?) -> Void)
{
Alamofire.request(.POST, webServicesURLPrefix + urlSuffix, parameters: params, encoding: .JSON, headers: self.headers)
request?.responseJSON { response in
switch response.result
{
case .Success:
success(response: response.result.value)
case .Failure(let error):
failure(error: error)
}
}
}
Call the method from anywhere as
self.postRequest("do-registration.php", params: params, filterParams: nil, success: { (response) -> Void in
self.afterResponse(response)
}) { (error) -> Void in
failure(error: error)
}
OR you can write a saperate method which you will have to call after the completion.
func afterResponse(responseData : AnyObject)
{
print("Done")
print(responseData)
}
You can cause the operation to be synchronous, but in order to do that you are going to have to use a semaphore for that you set up prior to the Alamofire request, and that you then release within the completion handler. You will wait on the semaphore right after you initiate Alamo fire.
There is a library Alamofire-Synchronous which works using semaphore.
Sample:
//json
let response = Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"]).responseJSON()
if let json = response.result.value {
print(json)
}
I have a class method, This method will take some time to get data from an http request.
Related part of the method:
do{
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions())
} catch {
print("Cound not serilize")
}
let task = session.dataTaskWithRequest(request) {
data, response, error in
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions()) as? NSDictionary
self.result = json! as? [String : AnyObject]
print(self.result)
It will print correct result from JSON, but in other side the object instance authGuestAPI will not:
let authGuestAPI = API(url: apiUrl, params: params, method: "POST")
authGuestAPI.run()
print("RESULT: \(authGuestAPI.result)")
It will print "RESULT: nil"
but If I put NSThread.sleepForTimeInterval(2) before print("RESULT: \(authGuestAPI.result)") It will print correct json data.
you need to create a function when you call the api request. Then implement a completion handler with that function. Add the code in the completion handler that you want to be executed when the value is eventually returned from the api.
I use SwiftyJSON and Alamofire to make download method with completionHandler to fix this issue.
func download(completionHandler: (JSON?, NSError?) -> ()) -> (){
Alamofire.request(.POST, url, parameters: self.params, encoding: ParameterEncoding.URLEncodedInURL , headers: self.header).responseJSON { (result: Response<AnyObject, NSError>) -> Void in
if result.result.isSuccess {
self.hasError = false
self.result = JSON(result.result.value!)
} else if result.result.isFailure {
self.errorCode = result.result.error!.code
self.errorMessage = result.result.error!.localizedDescription
print(result.result.error?.localizedDescription)
self.hasError = true
}
completionHandler(self.result, result.result.error)
}
}
I have a function that includes a responseObject in it's completion handler. At the moment this returns the json body when I call it, along with any errors.
Some parts of the API I am using however, don't return any data in the response body, they just return a response (200, 404, etc...)
I was thinking about appending the response inside the empty json object that is getting returned, then realised that would be silly and it would probably be better if I returned the NSHTTPURLResponse as well, but everything I have found just explains how to return the responseObject along with the error...
This is the function that returns the completion handler:
func makePostRequest(url : String, params : AnyObject, completionHandler: (responseObject: NSHTTPURLResponse, JSON?, error: NSError?) -> ()) -> Request? {
println("params = \(params)")
return Alamofire.request(.POST, url, parameters: params as? [String : AnyObject], encoding: .JSON)
.responseJSON { (request, response, data, error) in completionHandler(
//This is wrong
response: response as? NSHTTPURLResponse,
responseObject:
{
println("Request is \(request)")
println("Response is \(response)")
println("Data is \(data)")
println("Error is \(error)")
//Append the response to this JSON object?
//
var json:JSON = [:]
if let anError = error
{
println(error)
}
else if let data: AnyObject = data
{
json = JSON(data)
}
//probably better to return the two...
//
return (response, json)
}(),
error: error
)
}
}
And this is how its used:
networking.makePostRequest(documentUrl, params: docParams) { response, json, error in
println("response is: \(response)")
println("document json: \(json)")
println("document error: \(error)")
}
I've added in the 'response' bits to all the bits of code, i'm sure this is possible? just not sure how to achieve it..
For anyone stuck trying to figure out how to return stuff this way, I solved it like this:
func makePostRequest(url : String, params : AnyObject, completionHandler: (httpResponse: NSHTTPURLResponse, responseObject:JSON?, error: NSError?) -> ()) -> Request? {
println("params = \(params)")
return Alamofire.request(.POST, url, parameters: params as? [String : AnyObject], encoding: .JSON)
.responseJSON { (request, response, data, error) in completionHandler(
//This is wrong
httpResponse: response!,
responseObject:
{
println("Request is \(request)")
println("Response is \(response)")
println("Data is \(data)")
println("Error is \(error)")
//Append the response to this JSON object?
//
var json:JSON = [:]
if let anError = error
{
println(error)
}
else if let data: AnyObject = data
{
json = JSON(data)
}
return json
}(),
error: error
)
}
}
and then calling it like this:
networking.makePostRequest(workDocumentUrl, params: params) { response, json, error in
if response.statusCode == 200{
//do something
}
println("json: \(json)")
println("error: \(error)")
}