How to use Alamofire and SwiftyJSON with Swift? – Swift 3 – iOS 10 – Xcode 8

Swift JSON
Swift JSON

Alamofire is an HTTP networking library written in Swift.
SwiftyJSON makes it easy to deal with JSON data in Swift.

Steps to setup the CocoaPods

  1. Open Terminal
  2. CocoaPods runs on ruby so update your system.
  3. Install CocoaPods
    Install CocoaPods by using following command :

    If you are facing issue in EL Capitan or macOS Sierra then use following command :
  4. Setup the Pod

Note : This process will take some time.

Steps to add Alamofire and SwiftyJSON Pods to your project

  1. Open Terminal
  2. Navigate to the directory containing your AlamofireSwiftyJSONSample project by using the cd command:
  3. Give the init command
  4. It will create Podfile in your project’s directory.

  5. Open the Podfile using command
  6. Edit the pod file with your pods which you want to use and must remember the targets. Add pods to the particular target where you want to use that pod.
    In Swift you have to add one more line use_frameworks!
    So, Your Podfile will look like as :

  7. Go to terminal and give following command

Note : From now you have to use the .xcworkspace of your project.

Let’s use it in our project

Import it in your file where you want to use it.

Here is simple get request URL : http://api.androidhive.info/contacts/ we will use it to get data from it via Alamofire.

Here we have used conversion of data to the String.
We can do batter and go easy with the SwiftyJSON.

Let’s Populate a table with the JSON response

Declaration of variables and outlets:

Fetch data from Request and reload the table:
Using Alamofire Version 2.0.2:

Using Alamofire Version 3.3.1 :
Due to Alamofire Version 3 and syntax change old code not work.
You can check Alamofire 3.0 Migration Guide.

Using Alamofire Version 4.0 :
Due to Alamofire Version 4 and syntax change old code not work.
You can check Alamofire 4.0 Migration Guide.

Delegate methods of table :

Conclusion

Populated Table via Alamofire and SwiftyJSON
Populated Table via Alamofire and SwiftyJSON

This is a Sample of Alamofire with SwiftyJSON on Github.
Check other articles/tutorials for Swift Language

I have added one more tutorial regarding the wrapper for Alamofire and SwiftyJSON : How to create a wrapper for Alamofire and SwiftyJSON? Swift – iOS

Thank you all the watchers 🙂
Happy Coding 🙂
Keep Learning, Following, Swifting 😉