What is Quick Action?
Apple has introduced quick action from iOS 13 or later & it's a convenient way to perform useful, app specific actions right away from the home screen.
Apps can display Home Screen quick actions when users touch and hold the app icon (on a 3D Touch device, users press briefly on the icon). Here is an example for the health app quick actions.
How to add Quick Action?
There are two ways to add quick action for your app
Static Quick Actions
if your quick actions are pre-determined, define them as a list of shortcut items(quick action) in the `Info.plist` file.
Dynamic Quick Actions
if your quick actions are depend upon user-specific data or state of app,
define them by setting the `shortcutItems` property of the
shared `UIApplication` instance to an array of `UIApplicationShortcutItem` objects.
Let's get started with Static Quick Actions.
Let's consider, we need to add quick action for this Charts GitHub project . This project is mainly showing different types of rendering charts in iOS using Charts library. For more info on this project visit the Readme.md
Let's say we need to add quick action for going to BarChart page from the Home screen.
Steps involved:
Open the
ChartDemo.xcworkspace
project.Open the
Info.plist file
, and add a new key calledUIApplicationShortcutItems
, then set it to be an Array.Add new item to the array list of
UIApplicationShortcutItems
, which will get the name "Item 0", and set its data type to be Dictionary.Define your quick actions dictionary with following keys under the dictionary.
Here we have,
UIApplicationShortcutItemType
key is required field and this should be unique strings that will help in identify the quick Action. Let’s give “com.graphDemo.barChart”.
UIApplicationShortcutItemTitle
key is required and need to specify the title for quick action displayed to the user. Let’s give “Go to Bar Chart”.
UIApplicationShortcutItemIconType
is optional key and it defines the icon for the quick action. Let's give "barChart.jpg" which is already preset in assets folder.
Run the app, 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
Respond to triggered quick action
When users triggered the quick action from the home screen, it will be notified to the app by UIWindowSceneDelegate
.
- If your app is already loaded, the system calls the
windowScene(_:performActionFor:completionHandler:)
function of your scene delegate. - If the app isn’t already loaded, it’s launched and passes details of the shortcut item in through the connectionOptions parameter of the
scene(_:willConnectTo:options:)
function.
Steps to add triggered action for Go to Bar Chart
:
- Open
SceneDelegate.swift
file - Add this code snippet to
SceneDelegate.swift
file
func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
switch shortcutItem.type {
case "com.graphDemo.barChart":
// Go To Bar chart screen
guard let vc = UIStoryboard(name: "Main", bundle: .main).instantiateViewController(withIdentifier: "BarChartViewController") as? BarChartViewController else { return }
if let homeVC = window?.rootViewController as? UINavigationController {
homeVC.pushViewController(vc, animated: true)
}
default: break
}
}
- Run the app and trigger the
Go to Bar Chart
quick actions from the home screen, you should be able to navigate toBar Chart Screen
.