DEV Community

Filip Němeček
Filip Němeček

Posted on

iOS: How to add Quick Actions (shortcuts) to your app icon

iOS Quick Actions are meant as a shortcut for your users. They are triggered by long-pressing on the app icon. Each app can present up to four of them and they can be nice touch you can add with not that much a code.

We can have static actions that are defined in Info.plist or dynamic actions which are much more versatile. Let’s focus on the dynamic bunch. 🙂

Providing Quick Actions

We can use the class UIApplicationShortcutItem to define one shortcut item and then set all the items we want when app is moving to background.
For example this is one item from one of my recent apps to open management section:

UIApplicationShortcutItem(type: "OpenManagement", localizedTitle: "Management", localizedSubtitle: nil, icon: UIApplicationShortcutIcon(type: .favorite), userInfo: nil)

The type is used to differentiate the shortcut when user selects it. localizedTitle is the text on the shortcut and you can choose from variety of prepared icons. Here .favorite results in filled star. You can also provide your own.

There is also userInfo to provide dictionary with additional details.

Activating Quick Actions

Let’s use AppDelegate to activate the shortcut item shown above in the applicationWillResignActive method:

func applicationWillResignActive(_ application: UIApplication) {
        let managementShortcut = UIApplicationShortcutItem(type: "OpenManagement", localizedTitle: "Management", localizedSubtitle: nil, icon: UIApplicationShortcutIcon(type: .favorite), userInfo: nil)
        application.shortcutItems = [managementShortcut]
    }

The first part is done. Long-pressing on app icon will show this new Quick Action.

Responding to Quick Actions

There are two places in AppDelegate where we have to respond to Quick Action selection. One is the startup method didFinishLaunchingWithOptions where we can check the launchOptions like so:

if let shortCut = launchOptions?[UIApplication.LaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {
// handle shortCut here
}

And then there is also dedicated method called when the app is already running in the background:

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
        print(shortcutItem.type)
        // handle shortcutItem here
}

I would recommend creating helper method for handling shortcut items so you don’t have to duplicate logic in those two places.

Thanks for reading!

Is anything not clear? Do you want more information? Ask in the comments and I will do my best to help you.

Here is Quick Actions example from the Dashboardy app:

Quick Actions example in Dashboardy iOS app

Top comments (0)