For iOS Swift 4 :
go through this link :
http://ashishkakkad.com/2015/10/how-to-use-alamofire-and-swiftyjson-with-swift/
step 1 - install pod
pod 'Alamofire'
step 2 - import Alamofire (in your VC)
step 3 - create URL & parameters for that
let params:[String:String] = ["Name":self.name]
or
let params = nil
step 4 - call the func
public func GET_ALL(params:[String:String])
{
let baseURL = "your url"
if NetworkReachabilityManager()?.isReachable == true
{
AppDelegate.sharedInstance().ShowSpinnerView()
Alamofire.request(baseURL, method: .post, parameters: params, encoding: JSONEncoding.default, headers: nil).response { (response) in
AppDelegate.sharedInstance().HideSpinnerView()
do
{
let dict = try? JSONSerialization.jsonObject(with: response.data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSMutableDictionary
if dict?.count==0 || dict == nil
{
let alert = UIAlertController(title: APPNAME, message: "No reposnse from server.", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default, handler: { (action) in
})
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
else
{
self.checkDictionary(dict!)
if dict?.object(forKey: "data") is NSMutableDictionary
{
self.arrAllData = (dict?.value(forKeyPath: "data.myName") as? NSMutableArray)!
if(self.arrAllData.count == 0)
{
}
else
{
}
self.TableView.reloadData()
}
else
{
}
}
}
catch let error as NSError
{
print(error.localizedDescription)
let alert = UIAlertController(title: APPNAME, message: error.localizedDescription, preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default)
{ (action) in
}
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
self.hideSpinner()
}
}
}
else
{
AppDelegate.sharedInstance().HideSpinnerView()
let alert = UIAlertController(title: APPNAME, message: "No network available.", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default)
{ (action) in
}
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
}
let params:[String:String] = ["slug":self.strSlug]
let header = ["Content-Type":"application/x-www-form-urlencoded",
"Authorization": "Basic aW50cmFpZXAwMDE6dWZaeWFyUlp3NE1CeUg4RA=="
]
let url = API.GET_NEWS
**Info.plis : make sure you have added -> App transport Security Setting : Allow Arbitrary Loads : YES **
self.ArrData = dict?.value(forKeyPath: "results") as? NSMutableArray
for i in 0...self.ArrData.count-1{
let dict = self.ArrData[i] as? NSMutableDictionary
let dictGeometry = dict?.value(forKeyPath: "geometry") as? NSMutableDictionary
let TempDict = NSMutableDictionary()
TempDict.setObject(String(dictGeometry?.value(forKeyPath: "location.lat") as? Double ?? 0.0), forKey: "lat" as NSCopying)
TempDict.setObject(String(dictGeometry?.value(forKeyPath: "location.lng") as? Double ?? 0.0), forKey: "lng" as NSCopying)
TempDict.setObject(dict?.value(forKeyPath: "icon") as? String ?? "", forKey: "icon" as NSCopying)
TempDict.setObject(dict?.value(forKeyPath: "name") as? String ?? "", forKey: "name" as NSCopying)
TempDict.setObject(dict?.value(forKeyPath: "vicinity") as? String ?? "", forKey: "vicinity" as NSCopying)
print(TempDict)
self.ArrFinalData.add(TempDict)
}
print(self.ArrFinalData.count)
Swift Alamofire Method Calling:
func getMovieList(page: Int){
let url = API.SERVER_URL + "\(page)"
Alamofire.request(URL(string: url)!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { (responseObject) in
let response = self.formatResponse(data: responseObject)
let arr = response["results"] as! [[String : AnyObject]]
self.arrMovie.append(contentsOf: arr)
self.MoviewList = MovieDataList(data: self.arrMovie)
self.tableView.reloadData()
self.isLoadingList = false
self.deleteAllRecords()
}
}
func formatResponse(data:DataResponse<Any>)-> [String:AnyObject]
{
let responseObject = data.result.value as? [NSObject: AnyObject]
let response = responseObject as? [String : AnyObject]
return response ?? [:]
}
github.com/tej1107/MVC_MOVIEDEMO
github.com/tej1107/TableView_CollectionView
github.com/tej1107/TableView_CollectionView
github.com/tej11695/MVC_UserList
github.com/tej11695/New_MVC
github.com/tej11695/EmployeeRegistration
github.com/tej11695/List-SignUp_API
pod 'SDWebImage' pod 'Alamofire','4.9.1'