August 19, 2020

Home Screen Quick Action in iOS app

By Mohit Agrawal

Let’s see how to implement quick action features in your iOS app

Introduction

Welcome back to another iOS tutorial. Today’s tutorial is more about how to make your iOS app more advanced in terms of the user’s perspective. In this iOS tutorial, you will learn how to add implement home screen quick action feature in your iOS app.

What is Home screen quick action?

Apple has introduced many new features in iOS 13. In this release, 3D Touch, Haptic Touch, and long-press gestures (for all the devices without 3D Touch) are now one thing with a single-context menu system.

If you long-press the app icon then a quick action menu will pop up along the option of “Delete App” and “Edit Home Screen”.

home screen quick action ios

Implementation

You can implement quick actions in two ways. I will explain both ways and then you can easily decide which one is best for you. Two ways are:

  • Using the Info.plist, the quick action menu items will be static.
  • Using the programmatic approach also known as Dynamic Quick Action.

Static quick action menu

If the items in the quick action menu are not going to change then its good to use this way of implementation. Let’s take an example to understand this better.

We are going to design an app that will have two items in the quick action menu. The items are:

  • Color: It will change the background color of the view.
  • Share: It will allow users to share the app.

1 STEP: Open your Info.plist and add a new element UIApplicationShortcutItems. Make its type as an Array.

2 STEP: Under this element create an item for each Quick Action you want to add to your app and set the item as “Dictionary.” Now in this item, you have to add the following keys:

  • UIApplicationShortcutItemType (Required): Here we have to a unique string that will identify the quick action. Let’s give “ColorAction”.
  • UIApplicationShortcutItemTitle (Required): The title for quick action displayed to the user. Let’s give it “Color”.
  • UIApplicationShortcutItemSubtitle (Optional): Subtitle for the quick action. Let’s make it “Background color to gray”.
  • UIApplicationShortcutItemIconType (Optional): Here you can set a predefined icon made by Apple. I am using “UIApplicationShortcutIconTypeCompose”.

Similarly you can repeat the STEP 2 for another menu item.

quick action ios 2

Now run the app and you will be able to see the quick action menu. But it will not do anything because we have not implemented the click action on it. Let’s do it now

3 STEP: Adding a click action on quick action items is not much complicated. Open ScenceDelegate.swift and add a new method.

func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
        
        switch shortcutItem.type {
        case "ColorAction":
            window?.rootViewController?.view.backgroundColor = UIColor.gray
            break
        case "ShareAction":
            //code for share action
            break
        default:
            break
        }
    }

shortcutItem.type returns UIApplicationShortcutItemType which is a unique string identifier. Using this I have created a switch condition. Now you can easily identify the click and execute your code accordingly.

Dynamic quick action menu

The main advantage of creating a dynamic quick action menu button is that you add new or modify existing menu items according to your need.

func applicationWillResignActive(_ application: UIApplication) {
        application.shortcutItems = [UIApplicationShortcutItem(type: "ColorAction",
                                             localizedTitle: "Color",
                                             localizedSubtitle: "Changes the color",
                                             icon: UIApplicationShortcutIcon(type: .compose))]
    }

Create an array of UIApplicationShortcutItem and assign to UIApplication.shortcutItems as shown in the above code block.

Demo

Conclusion

This is how you can create a quick action menu in your iOS app. I hope this tutorial is useful for you guys. If you are having any problem while implementing then please let me know it in the comment section.

Subscribe YouTube: More tutorials like this

I hope this blog post is useful for you, do let me know your opinion in the comment section below.
I will be happy to see your comments down below 👏.
Thanks for reading!!!