This is sure to be asked several times, but I have not yet found the right answer, although I have been looking very hard.
I use Alamofire and SwiftyJSON and my JSON data looks like that:
{
"528" : {
"name" : "Name 1",
"id" : "528",
"product_id" : null,
"visible" : "0",
"level" : "2"
},
"29" : {
"name" : "Name 2",
"id" : "29",
"product_id" : null,
"visible" : "1",
"level" : "1"
},
"173" : {
"name" : "Name 3",
"id" : "173",
"product_id" : null,
"visible" : "0",
"level" : "2"
},
"143" : {
"name" : "Name 4",
"id" : "143",
"product_id" : null,
"visible" : "1",
"level" : "2"
},
...with this code:
Alamofire.request(.GET, dataURL, parameters: nil, encoding: .JSON)
.responseJSON { (request, response, jsonData, error) in
let json = JSON(jsonData!)
println(json)
}
...so everything should be fine with JSON
How can i access to that data? I mean how can i get names, ids, product_ids etc
How can i put that data (name) to my TableViewController?
I am using both SwiftyJSON and Alamofire in one of my projects. Here is how I am using it.
Create a protocol called APIProtocol.
Setup an API class with a GET method that accepts a delegate of type APIProtocol.
Setup TableViewController to implement the APIProtocol.
Call API.get() from TableViewController
Code
// Step 1
protocol APIProtocol {
func didReceiveResult(results: JSON)
}
// Step 2
func get(path: String, parameters: [String: AnyObject]? = nil, delegate: APIProtocol? = nil){
let url = "\(self.hostname)\(path)"
NSLog("Preparing for GET request to: \(url)")
Alamofire.request(.GET, url, parameters: parameters)
.responseJSON { (req, res, json, error) in
if(error != nil) {
NSLog("GET Error: \(error)")
println(res)
}
else {
var json = JSON(json!)
NSLog("GET Result: \(json)")
// Call delegate if it was passed into the call
if(delegate != nil) {
delegate!.didReceiveResult(json)
}
}
}
}
// Step 3
class ActivityViewController: UITableViewController, APIProtocol {
var activityModelList: NSMutableArray = [] // This is the array that my tableView is using.
...
func didReceiveResult(result: JSON) {
var activities: NSMutableArray = []
NSLog("Activity.didReceiveResult: \(result)")
for (index: String, activity: JSON) in result {
var activityModel = ActivityModel(
id: activity["id"].intValue,
message: activity["message"].stringValue
)
activities.addObject(activityModel)
}
// Set our array of new models
activityModelList = activities
// Make sure we are on the main thread, and update the UI.
dispatch_sync(dispatch_get_main_queue(), {
self.refreshControl!.endRefreshing()
self.tableView.reloadData()
})
}
}
// Step 4
override func viewDidLoad() {
MyAPI.get("/activities", delegate: self)
}
The accessors (for SwiftyJSON at least) work like:
if let userName = json[0]["528"]["name"].string{
println(userName) // "Name 1"
}
More information on how to use SwiftyJSOn can be found here in its official documentation: https://github.com/SwiftyJSON/SwiftyJSON
Regarding how to put that data into a UITableView, there are many methods. Set up a UITableView cell and then load in the JSON data in some sort of array for instance.
Check this repo for an example that declare how to use Alamofire in your code and how to create models with swift and parsing JSON response
Install Alamofire in your project
Import Alamofire in your class
Define these variables in you class
typealias complitionBlock = (data: AnyObject) -> Void
let KBASE_URL: String = "http://static.westwing.de/cms/dont-delete/programming_task/data.json"
Set the implementation for this function
func getMainItems(complition block: complitionBlock) {
Alamofire.request(.GET, KBASE_URL, parameters:nil).responseJSON { response in
do {
let items = try NSJSONSerialization.JSONObjectWithData(response.data!, options: NSJSONReadingOptions()) as! NSArray
let mutableArray: NSMutableArray = []
items.enumerateObjectsUsingBlock({ object, index, stop in
let str = object as! NSDictionary
//Replace WESItem with your model
//let item = WESItem(dictionary: str as NSDictionary)
mutableArray.addObject(item)
})
block(data: mutableArray)
} catch {
print(error)
}
}
}
For more information:
https://github.com/AhmedAskar/WestWing
Following should work for you :-
var nameArr = [String]()
Alamofire.request(.GET,"your url", parameters: nil)
.validate().responseJSON { response in
if let responseJson = response.result.value {
let name = responseJson["name"] as! String
nameArr.append(name)
}
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
})
}// Alamofire Close
Use tableview as you normally use it i.e.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell")
if cell == nil {
cell = UITableViewCell(style: .Default, reuseIdentifier: "cell")
}
cell!.textLabel?.text = nameArr[indexPath.row]
return cell!
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return nameArr.count
}
Note: No need to use Swifty JSON as Alamofire allows JSON response which can be directly handled within ".responseJSON".
pod 'Alamofire' pod 'SwiftyJSON' pod 'ReachabilitySwift'
import UIKit import Alamofire import SwiftyJSON import SystemConfiguration
class WebServiceHelper: NSObject {
typealias SuccessHandler = (JSON) -> Void
typealias FailureHandler = (Error) -> Void
// MARK: - Internet Connectivity
class func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
zeroAddress.sin_family = sa_family_t(AF_INET)
guard let defaultRouteReachability = withUnsafePointer(to: &zeroAddress, {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
SCNetworkReachabilityCreateWithAddress(nil, $0)
}
}) else {
return false
}
var flags: SCNetworkReachabilityFlags = []
if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) {
return false
}
let isReachable = flags.contains(.reachable)
let needsConnection = flags.contains(.connectionRequired)
return (isReachable && !needsConnection)
}
// MARK: - Helper Methods
class func getWebServiceCall(_ strURL : String, isShowLoader : Bool, success : #escaping SuccessHandler, failure : #escaping FailureHandler)
{
if isConnectedToNetwork() {
print(strURL)
if isShowLoader == true {
AppDelegate.getDelegate().showLoader()
}
Alamofire.request(strURL).responseJSON { (resObj) -> Void in
print(resObj)
if resObj.result.isSuccess {
let resJson = JSON(resObj.result.value!)
if isShowLoader == true {
AppDelegate.getDelegate().dismissLoader()
}
debugPrint(resJson)
success(resJson)
}
if resObj.result.isFailure {
let error : Error = resObj.result.error!
if isShowLoader == true {
AppDelegate.getDelegate().dismissLoader()
}
debugPrint(error)
failure(error)
}
}
}else {
CommonMethods.showAlertWithError("", strMessage: Messages.NO_NETWORK, withTarget: (AppDelegate.getDelegate().window!.rootViewController)!)
}
}
class func getWebServiceCall(_ strURL : String, params : [String : AnyObject]?, isShowLoader : Bool, success : #escaping SuccessHandler, failure :#escaping FailureHandler){
if isConnectedToNetwork() {
if isShowLoader == true {
AppDelegate.getDelegate().showLoader()
}
Alamofire.request(strURL, method: .get, parameters: params, encoding: JSONEncoding.default, headers: nil).responseJSON(completionHandler: {(resObj) -> Void in
print(resObj)
if resObj.result.isSuccess {
let resJson = JSON(resObj.result.value!)
if isShowLoader == true {
AppDelegate.getDelegate().dismissLoader()
}
success(resJson)
}
if resObj.result.isFailure {
let error : Error = resObj.result.error!
if isShowLoader == true {
AppDelegate.getDelegate().dismissLoader()
}
failure(error)
}
})
}
else {
CommonMethods.showAlertWithError("", strMessage: Messages.NO_NETWORK, withTarget: (AppDelegate.getDelegate().window!.rootViewController)!)
}
}
class func postWebServiceCall(_ strURL : String, params : [String : AnyObject]?, isShowLoader : Bool, success : #escaping SuccessHandler, failure :#escaping FailureHandler)
{
if isConnectedToNetwork()
{
if isShowLoader == true
{
AppDelegate.getDelegate().showLoader()
}
Alamofire.request(strURL, method: .post, parameters: params, encoding: JSONEncoding.default, headers: nil).responseJSON(completionHandler: {(resObj) -> Void in
print(resObj)
if resObj.result.isSuccess
{
let resJson = JSON(resObj.result.value!)
if isShowLoader == true
{
AppDelegate.getDelegate().dismissLoader()
}
success(resJson)
}
if resObj.result.isFailure
{
let error : Error = resObj.result.error!
if isShowLoader == true
{
AppDelegate.getDelegate().dismissLoader()
}
failure(error)
}
})
}else {
CommonMethods.showAlertWithError("", strMessage: Messages.NO_NETWORK, withTarget: (AppDelegate.getDelegate().window!.rootViewController)!)
}
}
class func postWebServiceCallWithImage(_ strURL : String, image : UIImage!, strImageParam : String, params : [String : AnyObject]?, isShowLoader : Bool, success : #escaping SuccessHandler, failure : #escaping FailureHandler)
{
if isConnectedToNetwork() {
if isShowLoader == true
{
AppDelegate.getDelegate().showLoader()
}
Alamofire.upload(
multipartFormData: { multipartFormData in
if let imageData = UIImageJPEGRepresentation(image, 0.5) {
multipartFormData.append(imageData, withName: "Image.jpg")
}
for (key, value) in params! {
let data = value as! String
multipartFormData.append(data.data(using: String.Encoding.utf8)!, withName: key)
print(multipartFormData)
}
},
to: strURL,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
//let datastring = String(data: response, encoding: String.Encoding.utf8)
// print(datastring)
}
case .failure(let encodingError):
print(encodingError)
if isShowLoader == true
{
AppDelegate.getDelegate().dismissLoader()
}
let error : NSError = encodingError as NSError
failure(error)
}
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { (response) -> Void in
if response.result.isSuccess
{
let resJson = JSON(response.result.value!)
if isShowLoader == true
{
AppDelegate.getDelegate().dismissLoader()
}
success(resJson)
}
if response.result.isFailure
{
let error : Error = response.result.error! as Error
if isShowLoader == true
{
AppDelegate.getDelegate().dismissLoader()
}
failure(error)
}
}
case .failure(let encodingError):
if isShowLoader == true
{
AppDelegate.getDelegate().dismissLoader()
}
let error : NSError = encodingError as NSError
failure(error)
}
}
)
}
else
{
CommonMethods.showAlertWithError("", strMessage: Messages.NO_NETWORK, withTarget: (AppDelegate.getDelegate().window!.rootViewController)!)
}
}
}
==================================
Call Method
let aParams : [String : String] = ["DeviceIDString", "DeviceType" : "iOS", ]
WebServiceHelper.postWebServiceCall(Constants.BaseURL, params: aParams as [String : AnyObject]?, isShowLoader: true, success: { (responceObj) in
if "\(responceObj["RespCode"])" != "1"
{
let alert = UIAlertController(title: Constants.kAppName, message: "\(responceObj["RespMsg"])", preferredStyle: UIAlertControllerStyle.alert)
let OKAction = UIAlertAction(title: "OK", style: .default) { (action:UIAlertAction!) in
}
alert.addAction(OKAction)
self.present(alert, animated: true, completion: nil)
}
else
{
let aParams : [String : String] = [
"Password" : self.dictAddLogin[AddLoginConstants.kPassword]!,
]
CommonMethods.saveCustomObject(aParams as AnyObject?, key: Constants.kLoginData)
}
}, failure:
{ (error) in
CommonMethods.showAlertWithError(Constants.kALERT_TITLE_Error, strMessage: error.localizedDescription,withTarget: (AppDelegate.getDelegate().window!.rootViewController)!)
})
}
// fatch json data swiftjson with alamofire
// First in using Alamofire
// second in fatch json data in tableView
// create json model classmodel1
//model2
Related
I have a problem with a webService call.
The problem is that when I call the service, and debug code, and print log in console, I'm sure my webService is only called once (log print once in console), but my request is apparently sent twice to the server and I have duplicate data in the list.
I know that it's not a server-side problem because it only happens on IOS (not Android).
Here is my code for call services:
public class PersistencyManager {
public func SendPostHttpRequest(baseURL: String, parameter: [String:Any], content: String, closure:#escaping ((_ success:JSON,_ error:NSError?) -> Void)) {
let manager = Alamofire.SessionManager.default
debugPrint("Request parameter ------>",parameter)
debugPrint(" Service URL -------> \(baseURL)")
if let url = URL(string: baseURL) {
var urlRequest = URLRequest(url: url)
urlRequest.setValue("text/html; charset=utf-8", forHTTPHeaderField: "Content-Type")
urlRequest.setURLEncodedFormData(parameters: parameter)
manager.request(urlRequest).responseJSON { response in
switch response.result {
case .success(let JSON) :
debugPrint("get Json response ---> \((JSON)) ")
closure(JSON,nil)
case .failure(let error):
closure(nil,error as NSError)
debugPrint("get error ---> \((error.localizedDescription)) ")
}
}
}
}
}
class LibraryAPI {
static let shareInstance : LibraryAPI = { LibraryAPI() }()
private let persistencyManager : PersistencyManager
init() {
persistencyManager = PersistencyManager()
}
func GetPostResponse(baseURL : String,parameters:[String:Any],contentType: String,closure:#escaping ((_ success:PersistencyManager.JSON,_ error:NSError?) -> Void)) {
persistencyManager.SendPostHttpRequest(baseURL: baseURL, parameter: parameters, content: contentType, closure: { success, error in
closure(success, error)
})
}
}
class TransactionAPI: TransactionProtocol {
static let shareInstance: TransactionAPI = {TransactionAPI()}()
func AddNewManagerRequest(_ parameter: [String : Any], closure: #escaping (([String : Any]?, NSError?) -> Void)) {
let url = Constants.BaseURL + Constants.K_NEWREQPORTERAGE
LibraryAPI.shareInstance.GetPostResponse(baseURL: url, parameters: parameter, contentType: "JSON", closure: {success,error in
var response: [String:Any]?
if let json = success as? [String: Any] {
response = json
}
closure(response, error)
})
}
}
class AddNewOrderViewController: MainViewController {
private func RegisterForNewPorterageRequest() {
let time = Utilities.shareInstance.GetSystemTime()
guard let userID = UserDefaults.standard.value(forKey: "user_id") as? String else {
return
}
StartActivity(activityColor: Constants.ACTIVITY_COLOR)
let token = TokenCreator.shareInstance.CreateTokenWithUserID(userID: userID, methodName: Constants.M_NEWREQUESTPORTERAGE)
request.tok = token
request.time = time
request.user_id = userID
let jsonModel = Utilities.shareInstance.GetJsonForm(objectClass: request)
TransactionAPI.shareInstance.AddNewManagerRequest(jsonModel, closure: {[weak self] success,error in
guard let strongSelf = self else{
return
}
if error != nil {
OperationQueue.main.addOperation {
strongSelf.StopActivity()
strongSelf.CreateCustomTopField(text: Constants.serverError, color: Constants.ERROR_COLOR)
}
}
else {
if let response = success {
debugPrint("add request service call once")
if let status = response["status"] as? String {
if status == "succ" {
OperationQueue.main.addOperation {
strongSelf.presentResultAlert()
}
}else {
OperationQueue.main.addOperation {
strongSelf.StopActivity()
strongSelf.CreateCustomTopField(text: Constants.send_data_error, color: Constants.ERROR_COLOR)
}
}
}
}
}
})
}
}
After adding log to server, I made sure my request was sent twice to server.
All console log print once in console.
I don't know when I call service twice, and why my request was sent twice to the server.
I don't understand how the log be displayed once, but the service has been called twice?
Any help appreciated.
It's really confusing, but it works perfectly with this method.
i have this method in persistencyMangerClass and i using this method instead SendPostHttpRequest.What really is the difference between these two methods. :|
public func SendMultiPartRequestWith(baseUrl: String, parameters: [String : Any],closure: #escaping ((_ success:JSON,_ error:NSError? ) -> Void)){
let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = 30
manager.session.configuration.timeoutIntervalForResource = 15
debugPrint(" Service URL -------> \(baseUrl)")
debugPrint("Request parameter ------>",parameters)
let headers: HTTPHeaders = [
"Content-type": "multipart/form-data"
]
manager.upload(multipartFormData: { (multipartFormData) in
for (key, value) in parameters {
if let data = value as? Data {
let fileName = (key as String) + ".jpg"
let mimType = (key as String) + "/jpg"
multipartFormData.append(data, withName: key as String, fileName: fileName, mimeType: mimType)
}
else {
if let v = value as? String {
multipartFormData.append("\(v)".data(using: String.Encoding.utf8)!, withName: key as String)
}else {
multipartFormData.append("".data(using: String.Encoding.utf8)!, withName: key as String)
}
}
}
}, usingThreshold: UInt64.init(), to: baseUrl, method: .post, headers: headers) { (result) in
switch result{
case .success(let upload, _, _):
upload.responseString { response in
if let err = response.error{
closure(nil, err as NSError)
return
}
if let JSON = response.result.value {
closure(JSON, nil)
}
}
case .failure(let error):
closure(nil, error as NSError)
}
}
}
I would like to post a JSON via Alamofire.
but i'm not too sure how can i deal with it.
my swiftyJSON is in an array as my
how can i encode an array of JSON into a dictionaryObject? to suits
Alamofire's Parameters?
urlRequest = try JSONEncoding.default.encode(urlRequest, with: location)
my sample JSON looks like this:
"[{\n acc = accuracy;\n lat = lat;\n long = long;\n type = type;\n}, {\n acc = accuracy;\n lat = lat;\n long = long;\n type = type;\n}, {\n acc = accuracy;\n lat = lat;\n long = long;\n type = type;\n}, {\n acc = accuracy;\n lat = lat;\n long = long;\n type = type;\n}, {\n acc = accuracy;\n lat = lat;\n long = long;\n type = type;\n}]"
The trick is you'll need to convert it to Dictionary.
Sample snippet as follow:
```
// let assume swiftyJSON is a SwiftyJSON object (JSON)
if let data = swiftyJSON.rawString()!.data(using: .utf8) {
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
Alamofire.request(url, method: .post, parameters: json, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
debugPrint(response)
}
} catch {
print("JSONSerialization error")
}
}
```
First add SwiftJSON to your project then
class func requestPOSTURL(serviceName:String,parameters: [String:Any]?, completionHandler: #escaping (JSON?, NSError?) -> ()) {
let headersSet: HTTPHeaders = [
"Authorization":GlobalAccesstoken,
"Accept": "application/json"
]
Alamofire.request(serviceName, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headersSet).responseJSON {
(response:DataResponse<Any>) in
switch(response.result) {
case .success(_):
if let data = response.result.value{
let json = JSON(data)
completionHandler(json,nil)
}
break
case .failure(_):
completionHandler(nil,response.result.error as NSError?)
break
}
}
}
AFWrapper.requestPOSTURL(serviceName: LapiUrl+"get_profile", parameters: params) { (response:JSON?, error:NSError?) in
if error != nil {
print(error!)
return
}
if response == nil {
return
}
print(response!)
var distRespoce = response!.dictionary?["response"]?.array?[0]
if (distRespoce?["status"].string == "true"){
let distuserData = distRespoce!.dictionary?["user_data"]
}
else{
print("no")
}
}
Try above code ..
You can do like this : -
Alamofire.request(urlString, method: .post, parameters: paramData, encoding:JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in
switch(response.result)
{
case .success(_):
if response.result.value != nil
{
do
{
var dict : NSDictionary = try JSONSerialization.jsonObject(with: response.data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
print(dict)
dict = UtilityClass.removeNullFromResponse(response: response.result.value! as! NSDictionary)
self.dataBlock(dict,nil)
}
catch
{
UtilityClass.hideHudLoading()
}
}
break
case .failure(_):
if response.result.error != nil
{
print(response.result.error!)
UtilityClass.hideHudLoading()
}
break
}
}
}
Simple Get post code, Easy to use, maintain and understand
Below is pod, you must need to use my code of json parsing.
#Network manager related
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :tag => ‘4.0.1’
pod 'AlamofireNetworkActivityIndicator', '~> 2.0'
pod 'AlamofireObjectMapper', '~> 4.0.0'
pod 'SVProgressHUD', :git => 'https://github.com/SVProgressHUD/SVProgressHUD.git'
pod 'Reachability', '~> 3.2'
pod 'SwiftyJSON', '~> 3.0.0'
pod 'ObjectMapper', '~> 2.0'
pod 'SDWebImage', '~> 3.8'
Here, You can Call API in your view controller.
RegistrationService().login(email: (txtEmail.text)!.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines), password: self.txtPassword.text!, success: { (response) in
//Save data to user default
}) { (error) in
print(error as Any)
}
RegistrationService.swift //Service class, in which you can add api's
//
// RegistrationService.swift
// hotelBids
//
// Created by Mehul Parmar on 27/05/16.
// Copyright © 2017 Sooryen Innovation labs. All rights reserved.
//
import Foundation
import Alamofire
import SwiftyJSON
import ObjectMapper
import AlamofireObjectMapper
open class RegistrationService {
enum listOfApi : String {
case
login,
country
}
func country(_ success:#escaping (_ responseObject:JSON) -> Void , failure:#escaping (_ errorResponse:JSON?) -> Void) {
// create post request
NetworkManager().Get(listOfApi.country.rawValue,
paramaters: [:],
showLoader: true,
success: { responseObject in
success(responseObject)
}) { error in
failure(error)
}
}
func login(email: String, password: String, success:#escaping (_ responseObject:JSON) -> Void , failure:#escaping (_ errorResponse:JSON?) -> Void) {
// create request parameter
let requestParameters = ["email" : email,
"password" : password,
"user_role" : "customer"
]
// create post request
NetworkManager().POST(listOfApi.login.rawValue,
paramaters: requestParameters as [String : AnyObject]?,
showLoader: true,
success: { responseObject in
success(responseObject)
}) { error in
failure(error)
}
}
NetworkManager.swift
//
// NetworkManager.swift
// Constants.kAppName.localized()
//
// Created by Mehul Parmar on 08/06/16.
// Copyright © 2016 . All rights reserved.
//
import Foundation
import Alamofire
import SVProgressHUD
import SwiftyJSON
//import AMSmoothAlert
import ObjectMapper
import AlamofireObjectMapper
//used for facebook logour while invalid session
import FacebookLogin
//MARK : - Errors
enum NetworkConnection {
case available
case notAvailable
}
class NetworkManager {
let baseUrlDev_OLD : String = "https://hotelbids.com/" + "dev/" + "hb-app/" + "v3/" + "user/"
let baseUrlDev : String = "http://184.73.131.211/api/v1/"
//MARK : - Shared Manager
let sharedManager = SessionManager()
func getHeaders(_ urlString: String) -> [String: String] {
var headerDictionary = [String: String]()
if UserDetail.rememberToken != nil {
if (UserDetail.rememberToken?.length)! > 0 {
headerDictionary[Constants.KEY_remember_token] = "\(UserDetail.rememberToken!)"
}
}
/*
if let xapi = UserDefault.getXapi() {
headerDictionary[Constants.KEY_Xapi] = xapi
}
if let accessLanguage = UserDefault.getLanguage() {
headerDictionary[Constants.KEY_Language] = accessLanguage
}
if let userId = UserDefault.getUserId() {
headerDictionary[Constants.KEY_USER_ID] = userId
}
if let accessToken = UserDefault.getAccessToken() {
headerDictionary[Constants.KEY_AccessToken] = accessToken
}*/
print("urlString: \(urlString)\nheaderDictionary : \(headerDictionary)")
return headerDictionary
}
func printResponse(urlString: String, paramaters: [String: AnyObject]?, response: AnyObject) {
let dictResponce = self.getValidatedData(response as AnyObject)
// if dictResponce.boolValue {
if let paramatersTemp = paramaters {
if paramatersTemp.values.count > 0 {
let jsonParameters = JSON(paramatersTemp)
print("\n\n\nurlString : \(urlString) ,\n\n paramaters: \(jsonParameters) ,\n\n Response: \(String(describing: dictResponce))\n\n\n")
}
else {
print("\n\n\nurlString : \(urlString) ,\n\n Response: \(String(describing: dictResponce))\n\n\n")
}
}
else {
print("\n\n\nurlString : \(urlString) ,\n\n Response: \(String(describing: dictResponce))\n\n\n")
}
// } else {
// print("urlString : \(urlString) ,\n Response: \(String(describing: dictResponce))")
// }
}
func getValidatedData(_ response: AnyObject?) -> JSON {
//Removing null and <null>, and replacing number or integer to string
guard var dictResponse = response as? NSDictionary else{
return nil
}
dictResponse = (dictResponse.replacingNullsWithStrings() as NSDictionary).convertingNumbersToStrings()! as NSDictionary
let jsonResponce = JSON(dictResponse)
return jsonResponce
}
// MARK: - Get Method
func Get(_ urlString: String, paramaters: [String: AnyObject]? = nil, showLoader: Bool? = nil, success:#escaping (_ responseObject:JSON) -> Void , failure:#escaping (_ errorResponse:JSON?) -> Void) {
switch checkInternetConnection() {
case .available:
if let showLoader = showLoader {
if showLoader {
DispatchQueue.main.async {
// update some UI
UIApplication.shared.keyWindow?.showLoader()
}
}
}
Alamofire.request(baseUrlDev.add(urlString: urlString), method: .get, parameters: paramaters, encoding: URLEncoding.default, headers: self.getHeaders(urlString)).responseJSON (completionHandler: { response in
DispatchQueue.main.async {
if UIApplication.shared.isIgnoringInteractionEvents {
UIApplication.shared.endIgnoringInteractionEvents()
}
if let showLoader = showLoader {
if showLoader {
if SVProgressHUD.isVisible() {
SVProgressHUD.dismiss()
}
}
}
}
//Success
if response.result.isSuccess {
if let value = response.result.value {
let dictResponce = self.isValidated(value as AnyObject)
//Print response using below method
self.printResponse(urlString: urlString, paramaters: paramaters, response: (value as AnyObject))
if dictResponce.0 == true {
success(dictResponce.1)
}
else {
failure(dictResponce.1)
}
}
}
else {
//Check response error using status code
if let strErrorReasonCode : Int = response.response?.statusCode {
if let data = response.data {
let jsonResponce = JSON(data: data)
if strErrorReasonCode == 500 {
print("\n\n\n\n server error :\(AppAlertMsg.kErrorMsg) \n\n URL:\(urlString) \n\n paramaters:\(JSON(paramaters as Any))\n\n\n\n")
UIApplication.shared.keyWindow?.makeToast(message: AppAlertMsg.kErrorMsg, duration: 3.0, position: "bottom" as AnyObject)
failure(jsonResponce)
return
}
if let dictionary : NSDictionary = jsonResponce.dictionaryObject as NSDictionary? {
let responce : ResponseDataModel = ModelManager.sharedInstance.getResponseDataModel(dictionary)
let authentication_Errors = 401
let authentication_Errors_Range = 400..<500
let Alamofire_Error = -1005
if authentication_Errors == strErrorReasonCode {
print("\n\n\n\nauthentication_Errors :\(strErrorReasonCode) \n\nmessage: \(responce.message) \n\nparamaters: \(String(describing: paramaters)) \n\nresponse: \(jsonResponce)\n\n\n\n")
self.isUnAuthotized()
failure(jsonResponce)
}
else if authentication_Errors_Range.contains(strErrorReasonCode) {
print("\n\n\n\nauthentication_Errors_Range :\(strErrorReasonCode) \n\nmessage: \(responce.message) \n\nparamaters: \(String(describing: paramaters)) \n\nresponse: \(jsonResponce)\n\n\n\n")
CustomAlert().ShowAlert(responce.message)
failure(jsonResponce)
}
else if authentication_Errors_Range.contains(Alamofire_Error) {
self.POST(urlString, paramaters: paramaters, showLoader: showLoader, success: { (responseObject) in
if response.result.isSuccess {
if let value = response.result.value {
let dictResponce = self.isValidated(value as AnyObject)
if dictResponce.0 == true {
success(dictResponce.1)
}
else {
failure(dictResponce.1)
}
}
}
}, failure: {_ in
failure(jsonResponce)
})
}
}
else {
print("\n\n\n\n server error :\(AppAlertMsg.kErrorMsg) \n\n URL:\(urlString) \n\n paramaters:\(JSON(paramaters as Any))\n\n\n\n")
UIApplication.shared.keyWindow?.makeToast(message: AppAlertMsg.kErrorMsg, duration: 3.0, position: "bottom" as AnyObject)
failure(jsonResponce)
}
}
}
else {
failure(nil)
}
}
})
case .notAvailable:
if let _ = showLoader {
UIApplication.shared.keyWindow?.makeToast(message: AppAlertMsg.kNoInternet, duration: 3.0, position: "bottom" as AnyObject)
}
failure(JSON(AppAlertMsg.kNoInternet))
print("No internet")
}
}
// MARK: - POST Method
func POST(_ urlString: String, paramaters: [String: AnyObject]? = nil, showLoader: Bool? = nil, success:#escaping (_ responseObject:JSON) -> Void , failure:#escaping (_ errorResponse:JSON?) -> Void) {
switch checkInternetConnection() {
case .available:
if let showLoader = showLoader {
if showLoader {
DispatchQueue.main.async(execute: {
if !UIApplication.shared.isIgnoringInteractionEvents {
UIApplication.shared.beginIgnoringInteractionEvents()
}
SVProgressHUD.show(withStatus: AppAlertMsg.kPleaseWait)
})
}
}
sharedManager.request(baseUrlDev.add(urlString: urlString), method: .post, parameters: paramaters, encoding: JSONEncoding.default, headers: self.getHeaders(urlString)).validate().responseJSON(completionHandler: { response in
DispatchQueue.main.async {
if UIApplication.shared.isIgnoringInteractionEvents {
UIApplication.shared.endIgnoringInteractionEvents()
}
if let showLoader = showLoader {
if showLoader {
if SVProgressHUD.isVisible() {
SVProgressHUD.dismiss()
}
}
}
}
//Success
if response.result.isSuccess {
if let value = response.result.value {
let dictResponce = self.isValidated(value as AnyObject)
//Print response using below method
self.printResponse(urlString: urlString, paramaters: paramaters, response: (value as AnyObject))
if dictResponce.0 == true {
success(dictResponce.1)
}
else {
failure(dictResponce.1)
}
}
} else {
//Check response error using status code
if let strErrorReasonCode : Int = response.response?.statusCode {
if let data = response.data {
let jsonResponce = JSON(data: data)
if strErrorReasonCode == 500 {
print("\n\n\n\n server error :\(AppAlertMsg.kErrorMsg) \n\n URL:\(urlString) \n\n paramaters:\(JSON(paramaters as Any))\n\n\n\n")
UIApplication.shared.keyWindow?.makeToast(message: AppAlertMsg.kErrorMsg, duration: 3.0, position: "bottom" as AnyObject)
failure(jsonResponce)
return
}
if let dictionary : NSDictionary = jsonResponce.dictionaryObject as NSDictionary? {
let responce : ResponseDataModel = ModelManager.sharedInstance.getResponseDataModel(dictionary)
let authentication_Errors = 401
let authentication_Errors_Range = 400..<500
let Alamofire_Error = -1005
if authentication_Errors == strErrorReasonCode {
print("\n\n\n\nauthentication_Errors (jsonResponce)\n\n\n\n")
self.isUnAuthotized()
failure(jsonResponce)
}
else if authentication_Errors_Range.contains(strErrorReasonCode) {
print("\n\n\n\nauthentication_Errors_Range :\(strErrorReasonCode) \n\nmessage: \(responce.message) \n\nparamaters: \(String(describing: paramaters)) \n\nresponse: \(jsonResponce)\n\n\n\n")
CustomAlert().ShowAlert(responce.message)
failure(jsonResponce)
}
else if authentication_Errors_Range.contains(Alamofire_Error) {
self.POST(urlString, paramaters: paramaters, showLoader: showLoader, success: { (responseObject) in
if response.result.isSuccess {
if let value = response.result.value {
let dictResponce = self.isValidated(value as AnyObject)
if dictResponce.0 == true {
success(dictResponce.1)
}
else {
failure(dictResponce.1)
}
}
}
}, failure: {_ in
failure(jsonResponce)
})
}
}
else {
print("\n\n\n\n server error :\(AppAlertMsg.kErrorMsg) \n\n URL:\(urlString) \n\n paramaters:\(JSON(paramaters as Any))\n\n\n\n")
UIApplication.shared.keyWindow?.makeToast(message: AppAlertMsg.kErrorMsg, duration: 3.0, position: "bottom" as AnyObject)
failure(jsonResponce)
}
}
}
else {
failure(nil)
}
}
})
case .notAvailable:
if let _ = showLoader {
UIApplication.shared.keyWindow?.makeToast(message: AppAlertMsg.kNoInternet, duration: 3.0, position: "bottom" as AnyObject)
}
failure(JSON(AppAlertMsg.kNoInternet))
print("No internet")
}
}
// MARK: - No Internet Connection
func checkInternetConnection() -> NetworkConnection {
if isNetworkAvailable() {
return .available
}
return .notAvailable
}
// MARK: - Check Status
func isValidated(_ response: AnyObject?) -> (Bool, JSON) {
//Removing null and <null>, and replacing number or integer to string
guard var dictResponse = response as? NSDictionary else{
return (false,nil)
}
dictResponse = (dictResponse.replacingNullsWithStrings() as NSDictionary).convertingNumbersToStrings()! as NSDictionary
let jsonResponce = JSON(dictResponse)
let responseModel : ResponseDataModel = ModelManager.sharedInstance.getResponseDataModel(dictResponse)
/* //
HTTP Status Code
200 – Success/OK
4xx – Authentication Errors
5xx – Service Errors
*/ //
guard let statusCodeInt = responseModel.code.toInt() else {
print("code is not proper")
return (false,jsonResponce)
}
let success_Range = 200..<300
let authentication_Errors = 401
let authentication_Errors_Range = 400..<500
let service_Errors_Range = 500..<600
if success_Range.contains(statusCodeInt) {
// all okey
return (true, jsonResponce)
}
else if authentication_Errors == statusCodeInt {
print("service_Errors_Range :\(statusCodeInt)")
self.isUnAuthotized()
return (false,jsonResponce)
}
else if authentication_Errors_Range.contains(statusCodeInt) {
print("authentication_Errors_Range :\(statusCodeInt)")
CustomAlert().ShowAlert(responseModel.message)
return (false,jsonResponce)
}
else if service_Errors_Range.contains(statusCodeInt) {
print("service_Errors_Range :\(statusCodeInt)")
CustomAlert().ShowAlert(responseModel.message)
return (false,jsonResponce)
}
else {
return (false,nil)
}
}
func isUnAuthotized() {
// we have to logout, bcos session expired , or user unauthorized
CustomAlert().ShowAlert(isCancelButton: false, strMessage: AppAlertMsg.kUnAuthotized) { (isYESClicked) in
if isYESClicked {
//Delete all data from user default
//Set sign in as Home screen
print("set to Login view controller ")
GmailClass.sharedInstance().signOut()
LoginManager().logOut()
UserStatus = UserType.Fresh.rawValue
let deviceTokenTemp = DeviceToken
if let bundle = Bundle.main.bundleIdentifier {
UserDefaults.standard.removePersistentDomain(forName: bundle)
}
DeviceToken = deviceTokenTemp
Constants.appDelegate.logoutSuccess()
}
}
}
// MARK: - Loader method
class func ShowActivityIndicatorInStatusBar() {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
class func HideActivityIndicatorInStatusBar() {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
}
extension String {
func add(urlString: String) -> URL {
return URL(string: self + urlString)!
}
func EncodingText() -> Data {
return self.data(using: String.Encoding.utf8, allowLossyConversion: false)!
}
}
I am new in iOS and I am bit confused that how to use singleton with Alamofire and how singleton is important. I created a networkWrapper class in that I have written Alamofire post and get method, but I didn't use singleton.
How can I create a Wrapper class of Alamofire with singleton? How can I get all tricks that is really important?
Below is my code for wrapper class:
import Foundation
import UIKit
import Alamofire
import SwiftyJSON
class AFWrapper: NSObject {
//TODO :-
/* Handle Time out request alamofire */
class func requestGETURL(_ strURL: String, success:#escaping (JSON) -> Void, failure:#escaping (Error) -> Void)
{
Alamofire.request(strURL).responseJSON { (responseObject) -> Void in
//print(responseObject)
if responseObject.result.isSuccess {
let resJson = JSON(responseObject.result.value!)
//let title = resJson["title"].string
//print(title!)
success(resJson)
}
if responseObject.result.isFailure {
let error : Error = responseObject.result.error!
failure(error)
}
}
}
static func requestPOSTURL(_ strURL : String, params : [String : AnyObject]?, headers : [String : String]?, success:#escaping (JSON) -> Void, failure:#escaping (Error) -> Void){
Alamofire.request(strURL, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).responseJSON { (responseObject) -> Void in
//print(responseObject)
if responseObject.result.isSuccess {
let resJson = JSON(responseObject.result.value!)
success(resJson)
}
if responseObject.result.isFailure {
let error : Error = responseObject.result.error!
failure(error)
}
}
}
}
In my controller:
if newLength == 6
{
let textZipCode = textField.text! + string
let dict = ["id" : "43","token": "2y103pfjNHbDewLl9OaAivWhvMUp4cWRXIpa399","zipcode" : textZipCode] as [String : Any]
//Call Service
AFWrapper.requestPOSTURL(HttpsUrl.Address, params: dict as [String : AnyObject]?, headers: nil, success: { (json) in
// success code
print(json)
}, failure: { (error) in
//error code
print(error)
})
setFields(city: "Ajmer", state: "Rajasthan", country: "India")
return newLength <= 6
}
I didn't look deep into your code. In swift we can create singleton by
static let sharedInstance = AFWrapper()
And it will create singleton instance of a class, so that class and static for singleton class instance functions are not necessary. Please refer below code for singleton class.
import Foundation
import UIKit
import Alamofire
import SwiftyJSON
class AFWrapper: NSObject {
static let sharedInstance = AFWrapper()
//TODO :-
/* Handle Time out request alamofire */
func requestGETURL(_ strURL: String, success:#escaping (JSON) -> Void, failure:#escaping (Error) -> Void)
{
Alamofire.request(strURL).responseJSON { (responseObject) -> Void in
//print(responseObject)
if responseObject.result.isSuccess {
let resJson = JSON(responseObject.result.value!)
//let title = resJson["title"].string
//print(title!)
success(resJson)
}
if responseObject.result.isFailure {
let error : Error = responseObject.result.error!
failure(error)
}
}
}
func requestPOSTURL(_ strURL : String, params : [String : AnyObject]?, headers : [String : String]?, success:#escaping (JSON) -> Void, failure:#escaping (Error) -> Void){
Alamofire.request(strURL, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).responseJSON { (responseObject) -> Void in
//print(responseObject)
if responseObject.result.isSuccess {
let resJson = JSON(responseObject.result.value!)
success(resJson)
}
if responseObject.result.isFailure {
let error : Error = responseObject.result.error!
failure(error)
}
}
}
}
Now you can call Singleton class instance function by
AFWrapper.sharedInstance.requestPOSTURL(HttpsUrl.Address, params: dict as [String : AnyObject]?, headers: nil, success: { (json) in
// success code
print(json)
}, failure: { (error) in
//error code
print(error)
})
May be you need that :
import UIKit
import Alamofire
struct FV_API
{
//URL is http://www.stack.com/index.php/signup
static let appBaseURL = "" // assign your base url suppose: http://www.stack.com/index.php
static let apiSignUP = "" // assign signup i.e: signup
}
class APIManager: NSObject
{
//MARK:- POST APIs
class func postAPI(_ apiURl:String, parameters:NSDictionary, completionHandler: #escaping (_ Result:AnyObject?, _ Error:NSError?) -> Void)
{
var strURL:String = FV_API.appBaseURL // it gives http://www.stack.com/index.php and apiURl is apiSignUP
if((apiURl as NSString).length > 0)
{
strURL = strURL + "/" + apiURl // this gives again http://www.stack.com/index.php/signup
}
_ = ["Content-Type": "application/x-www-form-urlencoded"]
print("URL -\(strURL),parameters - \(parameters)")
let api = Alamofire.request(strURL,method: .post, parameters: parameters as? [String : AnyObject], encoding: URLEncoding.default)
// ParameterEncoding.URL
api.responseJSON
{
response -> Void in
print(response)
if let JSON = response.result.value
{
print("JSON: \(JSON)")
completionHandler(JSON as AnyObject?, nil)
}
else if let ERROR = response.result.error
{
print("Error: \(ERROR)")
completionHandler(nil, ERROR as NSError?)
}
else
{
completionHandler(nil, NSError(domain: "error", code: 117, userInfo: nil))
}
}
}
In other NSObject I made that method i.e for Signup:
class SignUp: NSObject
{
class func registerWithAPI(firstName: String, lastName:String, completionHandler: #escaping (_ Result:AnyObject?, _ Error:NSError?) -> Void)
{
let dict = NSMutableDictionary()
if !firstName.isEmpty
{
dict.setValue(firstName, forKey: "firstname")
}
if !lastName.isEmpty
{
dict.setValue(lastName, forKey: "lastname")
}
APIManager.postAPI(FV_API.apiSignUP, parameters: dict)
{
(Result, Error) -> Void in
completionHandler(Result, Error)
}
}
}
In controller class I made method to call api like:
func apiForSignup()
{
SignUp.registerWithAPI(firstName: txtFieldFirstName.text!, lastName: txtFieldLastName.text!)
{
(Result, Error) -> Void in
// write code
}
Updated for Swift 4
import UIKit
import Alamofire
import SwiftyJSON
//
// MARK:- ipv6 Configuration...
//
private var webView = UIWebView(frame: CGRect.zero)
private var secretAgent: String? = webView.stringByEvaluatingJavaScript(from: "navigator.userAgent")
var authHeaders: HTTPHeaders = ["User-Agent": secretAgent!, "Content-Type": "application/json; charset=utf-8"]
class ApiManager: NSObject {
static let sharedInstance = ApiManager()
func requestGETURL(_ strURL: String, success:#escaping (JSON) -> Void, failure:#escaping (Error) -> Void) {
Alamofire.request(strURL).responseJSON { (responseObject) -> Void in
if responseObject.result.isSuccess, let resJson = responseObject.result.value {
success(JSON(resJson))
}
if responseObject.result.isFailure {
let error : Error = responseObject.result.error!
failure(error)
}
}
}
func requestPOSTURL(_ strURL: String, params: [String : Any]?, headers: [String : String]?, success:#escaping (JSON) -> Void, failure:#escaping (Error) -> Void) {
Alamofire.request(strURL, method: .post, parameters: params, encoding: JSONEncoding.default, headers: authHeaders).responseJSON { (responseObject) -> Void in
if responseObject.result.isSuccess, let resJson = responseObject.result.value {
success(JSON(resJson))
}
if responseObject.result.isFailure {
let error : Error = responseObject.result.error!
failure(error)
}
}
}
}
In my app i have tried to make different class for api calling. like click on login button and its call the method of different class. but when i want to go to another viewcontroller from that different class its getting crash.
here is my code in loginViewController
let mydata = DataControllerLogin()
mydata.login(txtemail.text!,password: txtPassword.text!)
class DataControllerLogin: UIViewController {
func login(username:String,password:String)
{
if Reachability.isConnectedToNetwork() == true
{
let url = "\(basicURL)login"
let param : [String : AnyObject] = [
"email" : username,
"password" : password
]
Alamofire.request(.POST, url, parameters: param, encoding: .JSON).responseObject(completionHandler: { (response:Response<LoginCode, NSError>) in
if (response.result.value != nil)
{
let LoginCode = response.result.value
let message = LoginCode?.Message
let detail = LoginCode?.result
if (LoginCode?.Status == 1)
{
let controller : LoginViewController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
self.navigationController?.pushViewController(controller, animated: true)
SVProgressHUD.dismiss()
}
else
{
alertViewShow(self, title: "Sorry", message: message!)
SVProgressHUD.dismiss()
}
if let threedayForecast = LoginCode?.result {
print(threedayForecast.FirstName)
}
}
else
{
}
})
}
else {
alertViewShow(self, title: "No Internet Connection", message: "Make sure your device is connected to the internet.")
}
}
}
but its getting crash on line wherever i have define viewController.
let controller : LoginViewController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
self.navigationController?.pushViewController(controller, animated: true)
its showing error like
exc_bad_instruction (code=exc_i386_invop subcode=0x0)
so if you know please let me know what is the issue?
Write this at the top of the file.
import UIKit
import Alamofire
import SwiftyJSON
typealias SOAPICompletionHandler = (code:Int, error:NSError?, response:NSDictionary?) -> Void
Add below method in your file:-
func callApi(strApiName:String, param : [String : AnyObject]?, type:String, header:[String : String]?, completionHandler:SOAPICompletionHandler) {
//let strURL : String = BASEURL+"/"+strApiName
let strURL = strApiName;
if type == POSTREQ {
Alamofire.request(.POST, strURL, parameters: param, encoding: .JSON, headers: header).responseJSON(completionHandler: { (responseData) -> Void in
let isSuccess = JSON(responseData.result.isSuccess)
if isSuccess {
// let swiftyJson = JSON(responseData.result.value! as! NSDictionary) as! AnyObject
completionHandler(code: 1, error: nil, response: responseData.result.value! as? NSDictionary)
} else {
let error = responseData.result.error! as NSError
completionHandler(code: 0, error: error, response: nil)
}
})
} else if type == GETREQ {
Alamofire.request(.GET, strURL, parameters: param, encoding: .JSON, headers: header).responseJSON(completionHandler: { (responseData) -> Void in
let isSuccess = JSON(responseData.result.isSuccess)
if isSuccess {
// let swiftyJson = JSON(responseData.result.value! as! NSDictionary)
completionHandler(code: 1, error: nil, response: responseData.result.value! as? NSDictionary)
} else {
let error = responseData.result.error! as NSError
completionHandler(code: 0, error: error, response: nil)
}
})
} else if type == PUTREQ{
Alamofire.request(.PUT, strURL, parameters: param, encoding: .JSON, headers: header).responseJSON(completionHandler: { (responseData) -> Void in
let isSuccess = JSON(responseData.result.isSuccess)
if isSuccess {
// let swiftyJson = JSON(responseData.result.value! as! NSDictionary)
completionHandler(code: 1, error: nil, response: responseData.result.value! as? NSDictionary)
} else {
let error = responseData.result.error! as NSError
completionHandler(code: 0, error: error, response: nil)
}
})
} else if type == DELETEREQ{
Alamofire.request(.DELETE, strURL, parameters: param, encoding: .JSON, headers: header).responseJSON(completionHandler: { (responseData) -> Void in
let isSuccess = JSON(responseData.result.isSuccess)
if isSuccess {
// let swiftyJson = JSON(responseData.result.value! as! NSDictionary)
completionHandler(code: 1, error: nil, response: responseData.result.value! as? NSDictionary)
} else {
let error = responseData.result.error! as NSError
completionHandler(code: 0, error: error, response: nil)
}
})
}
else if type == PATCHREQ{
Alamofire.request(.PATCH, strURL, parameters: param, encoding: .JSON, headers: header).responseJSON(completionHandler: { (responseData) -> Void in
let isSuccess = JSON(responseData.result.isSuccess)
if isSuccess {
// let swiftyJson = JSON(responseData.result.value! as! NSDictionary)
completionHandler(code: 1, error: nil, response: responseData.result.value! as? NSDictionary)
} else {
let error = responseData.result.error! as NSError
completionHandler(code: 0, error: error, response: nil)
}
})
}
}
you need to add Alamofire and SwiftyJSON.
Hope for best.
Following code I have written and I am getting response in JSON also but the type of JSON is "AnyObject" and I am not able to convert that into Array so that I can use that.
Alamofire.request(.POST, "MY URL", parameters:parameters, encoding: .JSON) .responseJSON
{
(request, response, JSON, error) in
println(JSON?)
}
The answer for Swift 2.0 Alamofire 3.0 should actually look more like this:
Alamofire.request(.POST, url, parameters: parameters, encoding:.JSON).responseJSON
{ response in switch response.result {
case .Success(let JSON):
print("Success with JSON: \(JSON)")
let response = JSON as! NSDictionary
//example if there is an id
let userId = response.objectForKey("id")!
case .Failure(let error):
print("Request failed with error: \(error)")
}
}
https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%203.0%20Migration%20Guide.md
UPDATE for Alamofire 4.0 and Swift 3.0 :
Alamofire.request(url, method: .post, parameters: parameters, 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 result = response.result.value {
let JSON = result as! NSDictionary
print(JSON)
}
}
like above mention you can use SwiftyJSON library and get your values like i have done below
Alamofire.request(.POST, "MY URL", parameters:parameters, encoding: .JSON) .responseJSON
{
(request, response, data, error) in
var json = JSON(data: data!)
println(json)
println(json["productList"][1])
}
my json product list return from script
{ "productList" :[
{"productName" : "PIZZA","id" : "1","productRate" : "120.00","productDescription" : "PIZZA AT 120Rs","productImage" : "uploads\/pizza.jpeg"},
{"productName" : "BURGER","id" : "2","productRate" : "100.00","productDescription" : "BURGER AT Rs 100","productImage" : "uploads/Burgers.jpg"}
]
}
output :
{
"productName" : "BURGER",
"id" : "2",
"productRate" : "100.00",
"productDescription" : "BURGER AT Rs 100",
"productImage" : "uploads/Burgers.jpg"
}
Swift 3, Alamofire 4.4, and SwiftyJSON:
Alamofire.request(url, method: .get)
.responseJSON { response in
if response.data != nil {
let json = JSON(data: response.data!)
let name = json["people"][0]["name"].string
if name != nil {
print(name!)
}
}
}
That will parse this JSON input:
{
people: [
{ name: 'John' },
{ name: 'Dave' }
]
}
I found the answer on GitHub for Swift2
https://github.com/Alamofire/Alamofire/issues/641
Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
.responseJSON { request, response, result in
switch result {
case .Success(let JSON):
print("Success with JSON: \(JSON)")
case .Failure(let data, let error):
print("Request failed with error: \(error)")
if let data = data {
print("Response data: \(NSString(data: data, encoding: NSUTF8StringEncoding)!)")
}
}
}
I'm neither a JSON expert nor a Swift expert, but the following is working for me. :) I have extracted the code from my current app, and only changed "MyLog to println", and indented with spaces to get it to show as a code block (hopefully I didn't break it).
func getServerCourseVersion(){
Alamofire.request(.GET,"\(PUBLIC_URL)/vtcver.php")
.responseJSON { (_,_, JSON, _) in
if let jsonResult = JSON as? Array<Dictionary<String,String>> {
let courseName = jsonResult[0]["courseName"]
let courseVersion = jsonResult[0]["courseVersion"]
let courseZipFile = jsonResult[0]["courseZipFile"]
println("JSON: courseName: \(courseName)")
println("JSON: courseVersion: \(courseVersion)")
println("JSON: courseZipFile: \(courseZipFile)")
}
}
}
Hope this helps.
Edit:
For reference, here is what my PHP Script returns:
[{"courseName": "Training Title","courseVersion": "1.01","courseZipFile": "101/files.zip"}]
Swift 5
class User: Decodable {
var name: String
var email: String
var token: String
enum CodingKeys: String, CodingKey {
case name
case email
case token
}
public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try container.decode(String.self, forKey: .name)
self.email = try container.decode(String.self, forKey: .email)
self.token = try container.decode(String.self, forKey: .token)
}
}
Alamofire API
Alamofire.request("url.endpoint/path", method: .get, parameters: params, encoding: URLEncoding.queryString, headers: nil)
.validate()
.responseJSON { response in
switch (response.result) {
case .success( _):
do {
let users = try JSONDecoder().decode([User].self, from: response.data!)
print(users)
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
case .failure(let error):
print("Request error: \(error.localizedDescription)")
}
swift 3
pod 'Alamofire', '~> 4.4'
pod 'SwiftyJSON'
File json format:
{
"codeAd": {
"dateExpire": "2017/12/11",
"codeRemoveAd":"1231243134"
}
}
import Alamofire
import SwiftyJSON
private func downloadJson() {
Alamofire.request("https://yourlinkdownloadjson/abc").responseJSON { response in
debugPrint(response)
if let json = response.data {
let data = JSON(data: json)
print("data\(data["codeAd"]["dateExpire"])")
print("data\(data["codeAd"]["codeRemoveAd"])")
}
}
}
This was build with Xcode 10.1 and Swift 4
Perfect combination "Alamofire"(4.8.1) and "SwiftyJSON"(4.2.0). First you should install both pods
pod 'Alamofire' and pod 'SwiftyJSON'
The server response in JSON format:
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip;q=1.0, compress;q=0.5",
"Accept-Language": "en;q=1.0",
"Host": "httpbin.org",
"User-Agent": "AlamoFire TEST/1.0 (com.ighost.AlamoFire-TEST; build:1; iOS 12.1.0) Alamofire/4.8.1"
},
"origin": "200.55.140.181, 200.55.140.181",
"url": "https://httpbin.org/get"
}
In this case I want print the "Host" info : "Host": "httpbin.org"
Alamofire.request("https://httpbin.org/get").validate().responseJSON { response in
switch response.result {
case .success:
print("Validation Successful)")
if let json = response.data {
do{
let data = try JSON(data: json)
let str = data["headers"]["Host"]
print("DATA PARSED: \(str)")
}
catch{
print("JSON Error")
}
}
case .failure(let error):
print(error)
}
}
Keep Calm and happy Code😎
I found a way to convert the response.result.value (inside an Alamofire responseJSON closure) into JSON format that I use in my app.
I'm using Alamofire 3 and Swift 2.2.
Here's the code I used:
Alamofire.request(.POST, requestString,
parameters: parameters,
encoding: .JSON,
headers: headers).validate(statusCode: 200..<303)
.validate(contentType: ["application/json"])
.responseJSON { (response) in
NSLog("response = \(response)")
switch response.result {
case .Success:
guard let resultValue = response.result.value else {
NSLog("Result value in response is nil")
completionHandler(response: nil)
return
}
let responseJSON = JSON(resultValue)
// I do any processing this function needs to do with the JSON here
// Here I call a completionHandler I wrote for the success case
break
case .Failure(let error):
NSLog("Error result: \(error)")
// Here I call a completionHandler I wrote for the failure case
return
}
I usually use Gloss library to serialize or deserialize JSON in iOS. For example, I have JSON that looks like this:
{"ABDC":[{"AB":"qwerty","CD":"uiop"}],[{"AB":"12334","CD":"asdf"}]}
First, I model the JSON array in Gloss struct:
Struct Struct_Name: Decodable {
let IJ: String?
let KL: String?
init?(json: JSON){
self.IJ = "AB" <~~ json
self.KL = "CD" <~~ json
}
}
And then in Alamofire responseJSON, I do this following thing:
Alamofire.request(url, method: .get, paramters: parametersURL).validate(contentType: ["application/json"]).responseJSON{ response in
switch response.result{
case .success (let data):
guard let value = data as? JSON,
let eventsArrayJSON = value["ABDC"] as? [JSON]
else { fatalError() }
let struct_name = [Struct_Name].from(jsonArray: eventsArrayJSON)//the JSON deserialization is done here, after this line you can do anything with your JSON
for i in 0 ..< Int((struct_name?.count)!) {
print((struct_name?[i].IJ!)!)
print((struct_name?[i].KL!)!)
}
break
case .failure(let error):
print("Error: \(error)")
break
}
}
The output from the code above:
qwerty
uiop
1234
asdf
in swift 5 we do like, Use typealias for the completion. Typlealias nothing just use to clean the code.
typealias response = (Bool,Any?)->()
static func postCall(_ url : String, param : [String : Any],completion : #escaping response){
Alamofire.request(url, method: .post, parameters: param, encoding: JSONEncoding.default, headers: [:]).responseJSON { (response) in
switch response.result {
case .success(let JSON):
print("\n\n Success value and JSON: \(JSON)")
case .failure(let error):
print("\n\n Request failed with error: \(error)")
}
}
}
The easy answer is to let AlamoFire do the decoding directly.
Surprisingly, you don't use the .responseJSON because that returns an untyped json object
Instead, you make your objects Decodable - and ask AF to decode directly to them
My json response contains an array of Account objects. I only care about the id and name keys (though there are many more)
struct Account:Codable {
let id:Int
let name:String
}
then simply
AF.request(url,
method: .get)
.responseDecodable(of:[Account].self) { response in
switch response.result {
case .success:
switch response.response?.statusCode {
case 200:
//response.value is of type [Account]
default:
//handle other cases
}
case let .failure(error):
//probably the decoding failed because your json doesn't match the expected format
}
}
let semaphore = DispatchSemaphore (value: 0)
var request = URLRequest(url: URL(string: Constant.localBaseurl2 + "compID")!,timeoutInterval: Double.infinity)
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let response = response {
let nsHTTPResponse = response as! HTTPURLResponse
print(nsHTTPResponse)
}
if let error = error {
print ("\(error)")
return
}
if let data = data {
DispatchQueue.main.async {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase//or any other Decoder\
do{
let jsonDecoder = JSONDecoder()
let memberRecord = try jsonDecoder.decode(COMPLAINTSVC.GetComplaints.self, from: data)
print(memberRecord.message)
for detailData in memberRecord.message{
print(detailData)
}
}catch{
print(error.localizedDescription)
}
}
}
semaphore.signal()
}
task.resume()
semaphore.wait()
}
pod 'Alamofire'
pod 'SwiftyJSON'
pod 'ReachabilitySwift'
import UIKit
import Alamofire
import SwiftyJSON
import SystemConfiguration
class WebServiceHelper: NSObject {
typealias SuccessHandler = (JSON) -> Void
typealias FailureHandler = (Error) -> Void
// MARK: - Internet Connectivity
class func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
zeroAddress.sin_family = sa_family_t(AF_INET)
guard let defaultRouteReachability = withUnsafePointer(to: &zeroAddress, {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
SCNetworkReachabilityCreateWithAddress(nil, $0)
}
}) else {
return false
}
var flags: SCNetworkReachabilityFlags = []
if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) {
return false
}
let isReachable = flags.contains(.reachable)
let needsConnection = flags.contains(.connectionRequired)
return (isReachable && !needsConnection)
}
// MARK: - Helper Methods
class func getWebServiceCall(_ strURL : String, isShowLoader : Bool, success : #escaping SuccessHandler, failure : #escaping FailureHandler)
{
if isConnectedToNetwork() {
print(strURL)
if isShowLoader == true {
AppDelegate.getDelegate().showLoader()
}
Alamofire.request(strURL).responseJSON { (resObj) -> Void in
print(resObj)
if resObj.result.isSuccess {
let resJson = JSON(resObj.result.value!)
if isShowLoader == true {
AppDelegate.getDelegate().dismissLoader()
}
debugPrint(resJson)
success(resJson)
}
if resObj.result.isFailure {
let error : Error = resObj.result.error!
if isShowLoader == true {
AppDelegate.getDelegate().dismissLoader()
}
debugPrint(error)
failure(error)
}
}
}else {
CommonMethods.showAlertWithError("", strMessage: Messages.NO_NETWORK, withTarget: (AppDelegate.getDelegate().window!.rootViewController)!)
}
}
class func getWebServiceCall(_ strURL : String, params : [String : AnyObject]?, isShowLoader : Bool, success : #escaping SuccessHandler, failure :#escaping FailureHandler){
if isConnectedToNetwork() {
if isShowLoader == true {
AppDelegate.getDelegate().showLoader()
}
Alamofire.request(strURL, method: .get, parameters: params, encoding: JSONEncoding.default, headers: nil).responseJSON(completionHandler: {(resObj) -> Void in
print(resObj)
if resObj.result.isSuccess {
let resJson = JSON(resObj.result.value!)
if isShowLoader == true {
AppDelegate.getDelegate().dismissLoader()
}
success(resJson)
}
if resObj.result.isFailure {
let error : Error = resObj.result.error!
if isShowLoader == true {
AppDelegate.getDelegate().dismissLoader()
}
failure(error)
}
})
}
else {
CommonMethods.showAlertWithError("", strMessage: Messages.NO_NETWORK, withTarget: (AppDelegate.getDelegate().window!.rootViewController)!)
}
}
class func postWebServiceCall(_ strURL : String, params : [String : AnyObject]?, isShowLoader : Bool, success : #escaping SuccessHandler, failure :#escaping FailureHandler)
{
if isConnectedToNetwork()
{
if isShowLoader == true
{
AppDelegate.getDelegate().showLoader()
}
Alamofire.request(strURL, method: .post, parameters: params, encoding: JSONEncoding.default, headers: nil).responseJSON(completionHandler: {(resObj) -> Void in
print(resObj)
if resObj.result.isSuccess
{
let resJson = JSON(resObj.result.value!)
if isShowLoader == true
{
AppDelegate.getDelegate().dismissLoader()
}
success(resJson)
}
if resObj.result.isFailure
{
let error : Error = resObj.result.error!
if isShowLoader == true
{
AppDelegate.getDelegate().dismissLoader()
}
failure(error)
}
})
}else {
CommonMethods.showAlertWithError("", strMessage: Messages.NO_NETWORK, withTarget: (AppDelegate.getDelegate().window!.rootViewController)!)
}
}
class func postWebServiceCallWithImage(_ strURL : String, image : UIImage!, strImageParam : String, params : [String : AnyObject]?, isShowLoader : Bool, success : #escaping SuccessHandler, failure : #escaping FailureHandler)
{
if isConnectedToNetwork() {
if isShowLoader == true
{
AppDelegate.getDelegate().showLoader()
}
Alamofire.upload(
multipartFormData: { multipartFormData in
if let imageData = UIImageJPEGRepresentation(image, 0.5) {
multipartFormData.append(imageData, withName: "Image.jpg")
}
for (key, value) in params! {
let data = value as! String
multipartFormData.append(data.data(using: String.Encoding.utf8)!, withName: key)
print(multipartFormData)
}
},
to: strURL,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
//let datastring = String(data: response, encoding: String.Encoding.utf8)
// print(datastring)
}
case .failure(let encodingError):
print(encodingError)
if isShowLoader == true
{
AppDelegate.getDelegate().dismissLoader()
}
let error : NSError = encodingError as NSError
failure(error)
}
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { (response) -> Void in
if response.result.isSuccess
{
let resJson = JSON(response.result.value!)
if isShowLoader == true
{
AppDelegate.getDelegate().dismissLoader()
}
success(resJson)
}
if response.result.isFailure
{
let error : Error = response.result.error! as Error
if isShowLoader == true
{
AppDelegate.getDelegate().dismissLoader()
}
failure(error)
}
}
case .failure(let encodingError):
if isShowLoader == true
{
AppDelegate.getDelegate().dismissLoader()
}
let error : NSError = encodingError as NSError
failure(error)
}
}
)
}
else
{
CommonMethods.showAlertWithError("", strMessage: Messages.NO_NETWORK, withTarget: (AppDelegate.getDelegate().window!.rootViewController)!)
}
}
}
==================================
Call Method
let aParams : [String : String] = [
"ReqCode" : Constants.kRequestCodeLogin,
]
WebServiceHelper.postWebServiceCall(Constants.BaseURL, params: aParams as [String : AnyObject]?, isShowLoader: true, success: { (responceObj) in
if "\(responceObj["RespCode"])" != "1"
{
let alert = UIAlertController(title: Constants.kAppName, message: "\(responceObj["RespMsg"])", preferredStyle: UIAlertControllerStyle.alert)
let OKAction = UIAlertAction(title: "OK", style: .default) { (action:UIAlertAction!) in
}
alert.addAction(OKAction)
self.present(alert, animated: true, completion: nil)
}
else
{
let aParams : [String : String] = [
"Password" : self.dictAddLogin[AddLoginConstants.kPassword]!,
]
CommonMethods.saveCustomObject(aParams as AnyObject?, key: Constants.kLoginData)
}
}, failure:
{ (error) in
CommonMethods.showAlertWithError(Constants.kALERT_TITLE_Error, strMessage: error.localizedDescription,withTarget: (AppDelegate.getDelegate().window!.rootViewController)!)
})
}