Home Screen Quick Actions — SwiftUI 2.0

Image for post
Image for post
Typical Home Screen Quick Actions

Home Screen Quick Actions are introduced in iOS 12. A shortcut buttons which navigates the user to a specific place in the application. Nowadays many apps are coming with this Quick Actions. In this post, we are going to learn how can we implement the “Home Screen Quick Actions” in SwiftUI App Life Cycle (2020).

Let’s Start

Types of Quick Actions

  1. Static
  2. Dynamic

Create Static Quick Actions

Static quick actions are defined on the build time. Static actions are never changing actions. It is defined in the project’s Info.plist file.

Right-click on Info.plist file → Open As → Source Code. Then, add below lines.

As you can see UIApplicationShortcutItems is used to add the static quick shortcut buttons. This is an array of a dictionary. In each dictionary UIApplicationShortcutItemType and UIApplicationShortcutItemTitle are mandatory keys.

UIApplicationShortcutItemType — Unique string represent for the quick action.

UIApplicationShortcutItemTitle — Title of the Quick Action button.

Check the definition of each key in this page.

At this stage, if you run the app, you should see the “Search” Quick action with Title, Subtitle and Icon.

Create Dynamic Quick Actions

Dynamic quick actions are added to the app at the run time. these quick actions are an array of UIApplicationShortcutItem assigned to the shared instance of UIApplication.

Let’s create a function in App struct AKA Entry point struct of the app in SwiftUI Life Cycle.

Here, we are creating four shortcuts — Call, Chat, Status, Contact.

We need to call this method when the app goes into the background state. So, we are using @Environment(\.scenePhase) environment variable and onChange(of:) modifier to capture the state of the app.

During the transition to a background state is a good time to update any dynamic quick actions because this code is always executed before the user returns to the Home screen.
- Apple

Apple official documentation instructing us that the dynamic quick actions can be added at the time of the app goes into the background phase. So, we are calling the addQuickActions() method in the .background

Refer this post for better understating of scene management in SwiftUI app life cycle.

At this stage, if you run the app you can see the “Search, Call, Chat, Status” quick actions.

One Static and three Dynamic quick actions.

Did something wrong?!🤔

Where is the “Contacts” quick action? 😳

Actually, we can add any number of quick actions but Apple will show a maximum of four shortcuts only.

Until now, we have seen how to add static and dynamic quick action buttons. Next step is how to pass data when selecting a quick action button?

Let’s see that.

Pass data via Quick Actions

To pass data into the app via quick actions, we need to slightly modify the addQuickActions() method. We are going to use the below constructor of UIApplicationShortcutItem.

Here, we are passing the data to the app through the userInfo dictionary.

Now, I have created four variables for each quick action and pass those into the UIApplicationShortcutItem. And if you observe, icons are added for each item.

Next step how to perform an action based on quick action. Let’s go 👇

Perform Action for Quick Action

There are two places we need to handle when the user tap on the quick action.

  1. If App completely closed, Opens freshly
  2. If App is in the background and tap on quick action.

For this, we need to implement two methods,

  1. application(_:configurationForConnecting:options:) — AppDelegate
  2. application(_:performActionFor:completionHandler:) — SceneDelegate

We know that SwiftUI App Life Cycle is not having AppDelegate and SceneDelegate. Let’s add AppDelegate first,

Add AppDelegate and SceneDelegate in SwiftUI App Life Cycle

Create a class named AppDelegate then add below snippet at the beginning of App struct. Just after, “@Environment(\.scenePhase) var phase”

Now, we have added AppDelegate to the SwiftUI Life Cycle. we can add any AppDelegate methods in the AppDelegate class.

Time to add SceneDelegate to the life cycle.

Inside the application(_:configurationForConnecting:options:) method configure the SceneDelegate class (CustomSceneDelegate) with UISceneConfiguration(name: sessionRole:)

Create UIApplicationShorcutItem variable outside the class. Coz, we need to use this variable in App struct, AppDelegate, CustomSceneDelegate classes.

Assign shortcutItemToProgess with options.shourtcutItem in application(_:configurationForConnecting:options:) method and shourtcutItem in application(_:performActionFor:completionHandler:) method.

Now, we have assigned the tapped quick action to a variable — shortcutItemToProcess. Next, we need to perform an action based on the shortcut item.

Every time, User tap on a quick action app will open. In other words, the .active scene phase will be triggered. So we need to perform the action in the .active switch inside the App struct. Modify the App struct like below,

get the “name” value from the shortcutItemToProcess variable. (line -10)

Navigate to the selected Quick Action View

In this part, we will navigate the corresponding view when the user selects a quick action. For that, we have to create the views.

I have created ListView instead of ContentView.

and I have created simple DetailView.

and an ObservableObject class QuickActionSettings

So, the Logic is we are using the QuickAction enum of QuickActionSettings as selection: value of NavigationLink in the ListView. If the QuickAction enum value (quickAction)changes then the NavigationLink will get fired as it is a Binding variable in ListView.

Now, All we need to do is, change the quickAction value based on the selected Quick Action. Let’s do the final step in the App struct.

Create an object for QuickActionSetttings outside the App Struct same as shortcutItemToProcess variable.

Then add the quickActionSettings Object as EnvironmentObject in ListView of App Struct. Final App struct will be like below,

Here, when the app is coming to the .active phase, we are assigning the quickAction value. which will fire the NavigationLink in ListView.

Let’s build and run the app. Check out the final output below,

Find the full GitHub repo here.

Hope you like this post. 🙏

Thank you. Happy learning 👨‍💻😀

Written by

Automation QA Engineer by profession. Learning iOS Development.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store