Quick Actions, that has been introduced with iPhone 6S, 6S Plus and iOS 9, allows users to access various parts of your app from the home screen in an easy and convenient way.

Recently we’ve built an app that uses dynamic Quick Actions, but the dynamic part of Quick Actions is generated from JSON that came from the API response. In other words, Quick Actions can be re-set every time the JSON response says so.

What are Quick actions in iOS?

Before we dive into the tutorial, let’s first review the technical stuff behind Quick Actions. Quick Actions can be static and dynamic. Static Quick Actions are configured in the info.plist, and they are defined at build time and visible to the user when they install or update the app. On the other side, the dynamic Quick Actions are added during run-time so the user will see them once they have launched the app (depending on predefined set of options in your app). Unfortunately, you can set only four actions, and static actions have priority. So be careful when mixing static and dynamic Quick Actions in your project.

What can I build using Quick actions in iOS?

In this example, we will work with our custom Animal model and our QuickActionsService class that will handle all of our Quick Actions stuff like registering and performing actions to the app.

Lets first take a look at our Animal model:

struct Animal {
let id: Int
let name: String
let description: String
let imageName: String

As you can see, the model contains the id and the name, just the stuff we need.
Let’s now go to our QuickActionsService class and create the method for mapping our model to the UIApplicationShortcutItem class and register our mapped models to the shortcutItems.

It is recommended to have some kind of prefix defined for the “type” property of the UIApplicationShortcutItem, just to be safe when handling action from user’s Quick action.

This is how we will define it:
static let defaultTypePrefix = “com.yourAppID.quickAction.”

Noticed the dot at the end of the string? Good.
Now we can proceed to the “register” method.

static func registerAnimalsToQuickActions(animals: [Animal]) {
UIApplication.shared.shortcutItems = animals.map { animal -> UIApplicationShortcutItem in
let type = QuickActionsService.defaultTypePrefix + "\(animal.id)"
return UIApplicationShortcutItem(type: type, localizedTitle: animal.name, localizedSubtitle: nil, icon: UIApplicationShortcutIcon(type: .favorite), userInfo: nil)

A very straight forward method, notice the use of “defaultTypePrefix” with animal.id – we’ll explain this later.

Assuming that you already have your models array, that’s it. The job of defining custom dynamic actions is done. Now we can proceed to performing the action from the app.

While still being in the QuickActionsService class, create new static functions called “performActionFor”:

static Func performActionFor(shortcutItem: UIApplicationShortcutItem, for root: ViewController)

Please notice that we are forwarding the shortcutItem and the root: ViewController variables to the method. The shortcutItem is the data that came from the AppDelegate, and the root is a reference to our top view controller, in this case, the only view controller in our example.

Depending on your needs and your coding style, your “performAction” method could look something like this (notice the type.contains defaultTypePrefix):

  if shortcutItem.type.contains(QuickActionsService.defaultTypePrefix) {
guard let idString = shortcutItem.type.components(separatedBy: ".").last, let id = Int(idString) else { return }
// now get original array from when we registered our quick actions and send it to the root controller class
let animal = JSONService.loadAnimals().first(where: { $0.id == id })
root.selectedAnimal = animal

But depending on your needs, you can configure this method so it suits your application.

Example implementation of Dynamic Quick actions in iOS on Github

Before we conclude this post, I encourage you to take a look at our implementation of dynamic quick action class at our git repo.
If you run the app from our git repo, you will be presented with a blank screen with the button on the bottom of the screen. By selecting the button the app will take 4 random items from our model array and set them to the Quick Actions.

Now try to activate Quick Actions from the home screen, you will be presented with a menu of 4 items that are registered with the “Register new actions” button, and if you select any of them you will be presented with the selected data.

For more information check Github.

If you like this post, maybe you’ll like this post on how to create simple today widget or the one where we create custom UISwitch.