Getting an OAuth 2.0 Token with Alamofire

OAuth 2.0 is super common for authentication these days, largely since it lets you login without giving your password to every app you use. If you’re not familiar with OAuth check out the great explanation on For example, if you wanted to let an iOS app have some access to your Twitter account, then the OAuth2 auth flow would be:

  1. The app sends you to Twitter to login
  2. You login to Twitter to authorize the app (possibly giving it specific limited permissions)
  3. Twitter sends you back to the app with a token for that app to use

The flow can be a little confusing (in fact, there’s an extra step that we’ll add later) but it means that the iOS app never knows your password. It also lets you revoke its permission later without changing your Twitter password.

When building any app build around an API with OAuth 2.0 authentication the first thing you need to do is setting up that login flow to get a token. So that’s what we’ll do today. We’ll set up a simple Swift app to access the GitHub API to get a list of our repositories on GitHub. Read on →

Objective-C in Swift Projects

While Swift is quickly gaining popularity there’s are still tons of great bits of code only available in Objective-C that you might want to use in your project. Today we’ll figure out how to use an Objective-C class in a Swift project. We’ll do it first by importing the files into the project manually, then we’ll do it using CocoaPods. Read on →

Changing API Call Parameters in One UITableView

We’ve set up a few different Swift tableviews that show results from a webservice:

Today we’ll improve on one of those projects to let the user choose different results to display in the table view. We’ll extend the Pull to Refresh Table View to have a segmented control so they can choose from a few different types of stocks to display, instead of just Apple, Google & Yahoo. Here’s what it’ll look like when we’re done:

Pull to Refresh Table View showing telecom stocks
Table View with Options
Read on →

App Extensions: Creating a Notification Center Widget

As of iOS 8 your app can add a section to the Notification Center that users see when they swipe down from the top of the screen. This view is called a Today extension or just a widget.

Today we’ll create a simple app with a today widget. It’ll just tell you the last time you ran the main app. Since extensions aren’t part of the app they’re associated with that’ll require:

  • a main app that saves the time it’s run
  • an extension app that displays it
  • and a data store that’s shared between the two apps
Read on →

Programmatic UITableView Scrolling

Scrolling to a certain point in a table view is a common requirement. For example, you might scroll to show new entries after loading new data or to show that a row has updated data. Since UITableView inherits from UIScrollView it’s easy to scroll programmatically.

In this demo we’ll set up a few different scrolling actions. To select where we want to scroll to we’ll use an Action Sheet, implementing as a UIAlertController with the .ActionSheet style. It’ll have a UIAlertAction for each of the scroll options. Read on →

NSNotifications in Swift

Today we'll build a cute little stock ticker that automatically updates every second. Here's what it'll look like when we're done:

Most of the app is the same as our pull to refresh project with a tableview that displays stock info from the Yahoo financial API. Originally we set it up to update using pull to refresh. Today we'll switch it to automatically update and use NSNotifications to make the data available to the tableview. Read on →

Pull to Refresh Table View in Swift

Adding pull to refresh to a UITableView sounds like a lot more work than it actually is. The iOS UIRefreshControl makes it a quick & easy feature to implement. Today we’ll toss together a quick stock price list app and then add pull to refresh to get up to the second updates on demand. When we’re done it’ll look like this:

Pull to Refresh Table View
Pull to Refresh Table View
Read on →

Alamofire Custom Headers

A reader trying to hook up to a Mashape API using Alamofire in Swift just emailed to ask how to handle custom headers (Thanks Claire!). She needs to pass 2 headers with her GET request:

  • X-Mashape-Key: MY_API_KEY
  • Accept: application/json

This curl statement works:

curl --get --include '' \
-H 'X-Mashape-Key: MY_API_KEY' \
-H 'Accept: application/json'

So how can we make this call work with Alamofire?

Read on →

Adding a Search Bar to a Table View in Swift

Previously we set up a Swift app that displayed info about Star Wars species from the Star Wars API using Alamofire. Today we’re going to add a search bar to the tableview so we can search through those species. We’ll start with a simple search of species names then add a scope bar to search other fields like the species classification and language.

Here’s what it’ll look like when we’re done the name search function:

Search Results
Name search function
Read on →

Custom Fonts in Swift

Eventually you’ll run into a designer who insists that the stock iOS fonts aren’t quite right for your app’s design. They’ll probably be right. Including fonts in iOS apps has gotten a lot easier in iOS8 since you can now preview them in your xibs & storyboards in Interface Builder.

First we’ll add the custom font to the Xcode project so we can use it in Interface Builder. Then we’ll set up the custom font programmatically in Swift code. We’ll programmatically set the font size based on the preferred font size that the user has selected in Settings.

Read on →