Custom Headers with Alamofire 4 and Swift 3

I previously wrote about adding custom headers to Alamofire 3 calls. Let’s figure out how to handle custom headers in Swift 3 and Alamofire 4.

When dealing with custom headers in Alamofire requests you might need to include a header for all of your API calls or just for a single call. We’ll show how to handle both of those scenarios and the four different ways that headers can be included in Alamofire calls.

The custom headers we set up previously were an API key and JSON accept header for the Mashape API:

  • X-Mashape-Key: MY_API_KEY
  • Accept: application/json
Read on →

Updating Alamofire Calls to Swift 3.0

Got a Swift project using Alamofire that needs to be migrated to Swift 3.0? Then you’ve probably figured out just how painful it can be if you try to just run the Xcode migration tool and figure out all of the errors. Changes to both Swift and Alamofire can make it tough to know just how to update those calls. So let’s look at a few common types of the Alamofire calls in Swift 2.2 and see how we can update them for Swift 3.0.

Then we’ll figure out how to create strongly typed objects in our code so that we’re not stuck passing JSON around everywhere.

Programmer hands on MacBook Pro keyboard
(image by #WOCinTech Chat)
Read on →

Updating NSURLSession Calls to Swift 3.0

Now that Xcode 8 is really here, it’s about time we thought about updating some of our code to Swift 3.0. If you’ve started migrating to Swift 3.0 you know it can be a real pain. I’ve got plans to update a bunch of posts but let’s start with the simplest stuff: Making networking calls using NSURLSession and parsing the resulting JSON.

The first thing you need to know is that it’s not called NSURLSession anymore. In an effort to streamline our code (and make everything harder to google), tons of NS prefixes have been removed in Swift 3.0.

Programmer doing mobile dev
(image by #WOCinTech Chat)
Read on →

Parsing NSDates in JSON from Web Services

Lots of web APIs give us dates but coercing them into a usable format can be a pain. We get something in our JSON like "2014-12-10T16:44:31.486000Z" or even just 1464192120 but we certainly can’t display those kinds of values to users. Today we’ll look at how to convert the dates that we get from APIs into NSDate objects then how to get nice display strings from those NSDate objects.

(image by Loic Djim)
Read on →

How Do Alamofire Routers Work?

Sometimes we read tutorials or books and the code seems like magic. It works but it’s really not clear why. Especially when it’s full of weird Swift stuff like enums and computed properties and pattern matching. It’s enough to make you want to curl up in bed and give up on programming altogether.

Pug curled up in blanket in bed
How “magic” code makes me feel (image by Matthew Wiebe)

A reader pointed out recently that my Alamofire router code is guilty of showing fancy code with funky Swift features. And the blog post doesn’t make it clear what’s happening. So today I’ll make things right and we’ll figure out exactly how something like Alamofire.request(Router.Get(1)).responseJSON‚Ķ actually works.

Read on →

Using a Router to Organize your Alamofire API Calls

Previously we set up some REST API Calls With Alamofire & SwiftyJSON. While it’s a bit of overkill for those simple calls we can improve our code by using an Alamofire router. The router will put together the URL requests for us which will avoid having URL strings throughout our code. A router can also be used to apply headers, e.g., for including an OAuth token or other authorization header.

Yes, I see the date on this post. But well organized code is no joke. Be nice to your future self by making it easy to understand and work with your code. You’ll thank yourself later.

Thank you written on shop door
(image by Matt Jones)

Read on →

Getting to Know UITextField

Apple gives us a few options for displaying and accepting text in iOS apps. Sometimes it can be confusing whether you should use a UILabel, a UITextField, or a UITextView.

If you’re just displaying static text, use a UILabel. You’ll probably come across recommendations to use a UITextView for special formatting. That used to be a good idea but nowadays UILabel with an attributed string can do almost everything a UITextView can do. Try it with the label first and only switch to a text view if you absolutely need to.

If you’re accepting user input then you’ll have to choose between UITextField and UITextView. If there’s only a single line of text, use a UITextField. For multiple lines of input use a UITextView.

Read on →

Building URLs with NSURLComponents and NSURLQueryItems

Creating URLs from strings is a minefield for bugs. Just miss a single / or accidentally URL encode the ? in a query and your API call will fail and your app won’t have any data to display (or even crash if you didn’t anticipate that possibility). Since iOS 8 there’s a better way to build URLs using NSURLComponents and NSURLQueryItems. Let’s look at how to do that and when it might be fine to keep using NSURL(string: urlString).

Read on →