Categories
iOS

Handling iOS 13 Quick Actions

iOS 13 standardized home screen quick actions across all devices by getting rid of 3D touches in favor of haptic touch gestures. One positive thing about this change is the elimination of gesture collisions between long-press and force touch press gestures.

In the past, it used to be tricky, since opening quick actions required a force touch, while uninstalling applications required the normal press to bring the jiggle effect. Now that the 3D touch is no more, accessing quick actions is smoother and faster than ever before.

Home screen quick actions provide a convenient way of navigating to a particular part of the applications by setting up shortcut menus that can be viewed by long-pressing the app’s launcher icon. With iOS 13, the quick actions now provide the Delete, Rearrange, and Share App actions implicitly in every quick action menu.

Moreover, we can further enhance the user experience by displaying a widget with the quick action menu. The widget extension is useful in displaying useful information (such as headlines, wallet money, or stock prices) to the user while also providing the ability to be added to Today’s Widget screen.

Our Goal

In the next few sections, we’ll be exploring the different ways of creating Quick Actions and ways to handle the menu selections.

Furthermore, we’ll integrate a widget alongside our actions menu which is a fairly common design in iOS 13 applications.

Quick Action Menus

Quick Actions Menus can be two types — static or dynamic.

The static ones never change and are defined in the Info.plist file under the UIApplicationShortcutItems —the key which holds an array of dictionaries for the menu items. On the other hand, dynamic menu actions are used when your menu actions are user or session-specific, such as displaying most visited or last visited screens.

Let’s look at how each of these quick action types is implemented in an iOS Application. Create a new Xcode project to get started.

Static Quick Actions

Static Quick Actions are defined in the Info.plist file in the following way:

ios13-quick-actions-info-plist

Every item in the UIApplicationShortcutItems array of dictionaries must have their own UIApplicationShortcutItemTitle and UIApplicationShortcutItemType properties defined.

Optionally, use the UIApplicationShortcutItemSubtitle for the description. The IconType or IconFile can be used to set the icon of the menu item. The icon color is monochromatic, hence it is recommended to not use emoji icons for the menus.

Additionally, we can save the data for each item in the UIApplicationShortcutItemUserInfo dictionary. The userInfo values need to conform to the NSSecureCoding protocol.

The following illustration showcases an example of Static Quick Actions from the Info.plist that we defined above:

ios13-quick-actions-output-1

Dynamic Quick Actions

Dynamic Quick Actions require the application to be loaded at least once. Typically, these actions should be created in the AppDelegate that’s triggered when the user returns to the home screen, thereby retrieving any necessary application data and transforming it into dynamic actions accordingly.

We can create the UIApplicationShortcutItem for each context menu item here and set the array to the UIApplication’s shortcutItems property as shown below:

func applicationWillResignActive(_ application: UIApplication) {
application.shortcutItems = [
UIApplicationShortcutItem(
type: "DynamicAction",
localizedTitle: "Name",
localizedSubtitle: nil,
icon: UIApplicationShortcutIcon(type: .contact),
userInfo: nil
)]
}

Responding to Quick Action Selections

When an item in the quick actions context menu is pressed, either of the following happens:

  • If the app was not running — didFinishLaunchingWithOptions method inside the AppDelegate class is called. We can handle the action of the menu item by retrieving the shortcutItem from the launchOptions dictionary with the key UIApplication.LaunchOptionsKey.shortcutItem .
  • If the app was already running — for iOS 13, SceneDelegate does all the heavy lifting. So the shortcutItem that was clicked gets triggers the following method of the SceneDelegate class:
func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void){
//Handle actions based on shortcutItem.type
}

For iOS 12 and older versions, the equivalent AppDelegate method gets triggered.

Integrating Today’s Widget Extension

Widgets provide quick access to the application by providing certain realtime display views outside of the main app. We’ll be adding a Today’s Widget Extension in our application in order to show this with the Quick Actions Menu.

To set up the extension, click on + to add a new target in your Xcode and select today’s extension. This would create the relevant files for the extension (a ViewController, storyboard, and Info.plist file).

Next, add a URL type in your main application’s target. We’ll use this URL scheme for launching the application from the widget.

ios13-quick-actions-info-plist-add

In our TodayViewController.swift file, we’ve replaced the UILabel with a UIButton. In order to launch the containing app from the widget, we need to open the URL on button tap, as shown below:

import UIKit
import NotificationCenter

class TodayViewController: UIViewController, NCWidgetProviding {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func onButtonTap(_ sender: Any) {
        self.extensionContext?.open(URL(string: "iOS13QuickActions://")!)
    }
   
    func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
        completionHandler(NCUpdateResult.newData)
    }   
}

We can even enable the app groups’ capability in Xcode to enable data sharing across the widget and the iOS application.

In return, we get the following outcome from our application when we did a long press on the icon:

ios13-quick-actions-output-2

Conclusion

Quick action menus display the “Share App” action only for apps that are available in the App Store. Besides, we saw how today’s extension and the quick action menu provide fast access to the contents of the application.

You can download the full source code from the Github Repository.

By Anupam Chugh

iOS Developer exploring the depths of ML and AR on Mobile.
Loves writing about thoughts, technology, and code.

Leave a Reply

Your email address will not be published. Required fields are marked *