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(TodoRouter.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. 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.

Updated to Swift 3.1 and Alamofire 4.4.

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 →

Transparent Table View with a Background Image

A background image behind a table view can make your app look nicely customized without a ton of effort. There are a few pitfalls to make it look just right so let’s walk through how to add a background image to a UITableView. We’ll tweak the transparency so the table view cells look ok, stop extra empty cells from hiding the background image, and center & scale the image so it doesn’t get stretched.

Read on →



Parsing HTML and Downloading Files in Swift 2.0

While web APIs are getting more and more common some data at some point you’ll find yourself working with data that’s only available in HTML. I have a problem like this right now: NOAA makes all of their boating charts (depth and navigation maps) available online for free. But the index of files is just a huge HTML list or a funky web viewer. I want to download some charts to use offline but building an app to do that would require parsing the HTML list. The charts that I want are the Booklet format ones for the Atlantic coast.

HTML parsing support in iOS isn’t very good so handling tasks like this one can end up being quite a challenge. We’ll use the HTMLReader library to make it easier. Our app will have a table view listing the chart names and numbers. Tapping on a row will download the PDF chart. We’ll use Alamofire to download the PDF files and save them locally. And we’ll include an “Open In” function since the default PDF apps on iOS choke on PDFs with as much detail as these ones. Read on →


Saving Data to Show Offline

Sometimes we want to keep data between runs of our apps. NSUserDefaults is fine for a little bit of data. If there’s a lot of data then a full blown database makes sense, maybe Core Data, SQLite or Realm. But what if it’s a middling amount of data? Then use NSKeyedArchiver. It’s a lot simpler to set up than a database and can hold a lot more data than NSUserDefaults. If you don’t need fancy database features like querying then NSKeyedArchiver will save you a lot of coding time.

Today we’ll use NSKeyedArchiver to handle not having an internet connection in our Stock Quote Page View Controller demo app. We’ll do that by persisting the stock quotes each time we fetch them from the web service. Read on →