In this blog, we are going to talk about UIApplicationShortcutItem. It specifies a user-initiated action for your application. It was introduced in iOS 12 and is also known as Home Screen Dynamic Quick Action. This works by 3D touching of your application or by long-pressing the app on the menu page. There are 2 ways in which you can add UIApplicationShortcutItem to your application. Let’s see each way one by one.
Ways to implement Shortcut in application:
1. Static Type
We can add a static shortcut via Info.plist
file in the application. First, open the Info.plist file and add a new key with name UIApplicationShortcutItems
and make its type to array
. Then add an item to this array “Item 0” and make it type to be dictionary
type. Now, add below mentioned 3 keys with String
data type.
- Firstly, add
UIApplicationShortcutItemIconType
and its value should be one of the build-in icon types. Here are some example of build-in icon types:UIApplicationShortcutIconTypeCompose
,UIApplicationShortcutIconTypePlay
,UIApplicationShortcutIconTypeSearch
,UIApplicationShortcutIconTypeLove
,UIApplicationShortcutIconTypeShare
, orUIApplicationShortcutIconTypeAlarm
. - Then, add
UIApplicationShortcutItemTitle
and its value should be title of your quick action - Finally, add
UIApplicationShortcutItemType
and its value should be a unique identifer which identifes your action. Generally the identifer should be followed by your application bundle identifier, for eg: com.yourapp.name.shortcut
2. Dynamic Type
We can add dynamic type shortcuts by code. For this, you need to assign a list of UIApplicationShortcutItem
to UIApplication.shared.shortcutItems
.
1 2 3 |
let icon = UIApplicationShortcutIcon(type: .alarm) let item = UIApplicationShortcutItem(type: "com.yourapp.name.shortcut", localizedTitle: "Add Shortcut", localizedSubtitle: "subtitle", icon: UIApplicationShortcutIcon(type: .audio), userInfo: nil) UIApplication.shared.shortcutItems = [item] |
Handling Quick Action Event
For handling Quick Action we need to check the launchOptions
dictionary of didFinishLaunchingWithOptions
method.
1 2 3 4 5 6 7 8 |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { if let shortcutItem = launchOptions?[UIApplication.LaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem { if shortcutItem.type == "com.yourapp.name.shortcut" { // handle action } } return true } |
Thank you for reading this article. After that, if you want to read more articles regarding iOS Development click here.