Like A Girl

Pushing the conversation on gender equality.

Code Like A Girl

3 steps to make your first API call

Most apps involve an API call at some stage. If you haven’t tried calling an API yet, it’s a great thing to learn.

Here are three steps to start your API journey.

  1. Install Alamofire
  2. Install SwiftyJSON
  3. Change your App Transport Security Settings in Xcode

Let’s go through them one by one.

1. Install Alamofire

This can be done using Carthage or CocoaPods. In my opinion, CocoaPods is easier for a newbie. You can read about the difference in this post. If this is your first time installing a Cocoapod, this post takes you through how to do it step by step.

Alamofire is a great way to easily make network calls, aka API calls. It simplifies the swift code you need to write.

I remember finding it really challenging to find a code snippet I could use and understand with Alamofire. Here’s what I do at the moment. Ignore the empty completion handler for now. That’s another post for the future.

// At the top of the swift file, just after the comments
import Alamofire
import SwiftyJSON
// Somewhere inside the swift file
class func loadStarWarsPeopleData( completionHandler: @escaping (Void) -> (Void)) {
  let urlString = "http://swapi.co/api/people/"
  Alamofire.request(urlString).responseData { (dataResponse) in
    if let data = dataResponse.result.value {
      // do something with the data that
// Alamofire has pulled out for you
      completionHandler()
}
}
}

2. Install SwiftyJSON

Everyone pairs SwiftyJSON with Alamofire. Again it simplifies the Swift 3 code into something that’s more readable and easier to write.

The SwiftyJSON part of the code replaces the comments in the above block.

if let data = dataResponse.result.value {
  let json = JSON(data: data)
let arrayOfPeople = Person.parseDataFromJson(json: json)
  completionHandler()
}

You should have a Person.swift file, called a model. This will also require import SwiftyJSON. The code you will use there will look something like this:

class func parseDataFromJson(json: JSON) -> [Person] {
var newPerson: Person
var arrayOfPeople: [Person] = []
  if let arrayOfAllAttributes = json["results"].array {
    for person in arrayOfAllAttributes {
let name = person["name"].string ?? ""
let height = person["height"].string ?? "0"
      newPerson = Person(name: name, height: height)!
arrayOfPeople.append(newPerson)
}
}
  return arrayOfPeople
}

3. Change your App Transport Security Settings

The newer versions of Xcode have locked down the ability to access API calls over the internet by default. If you want to retrieve anything from a URL, you need to change your App Transport Security Settings to allow this.

See this Stackoverflow article, as ‘Undo’ did a great job of explaining it visually.

Copied from Stackoverflow

Below is one more step (step 4) that the yellow text did not explain as clearly.

  1. Right click on “Bundle OS Type Code”
  2. Select “Add a row”
  3. Select “App Transport Security Settings”. You can start typing this into the field and it should come up.
  4. Click on the + button next to “App Transport Security Settings” or the down arrow to the left of it. “Allow arbitrary loads” should come up as a new row. However it took me a few tries before this happened.
  5. Change the column to “YES”.

This should get you a good part of the way to making your first API call. The next stage is to understand completion handlers and the grand dispatch queue. I’ll link to those posts when I write about them. Good luck!