Swift version: 5.10
There are two ways to add a shortcut item for 3D Touch: you can register a list of static items that always get shown, or you can create a dynamic list in code based on user information.
Let's start by tackling static lists. Open your Info.plist file, and add a new key called UIApplicationShortcutItems
, then set it to be an Array. Add one new item in there, which will get the name "Item 0", and set its data type to be Dictionary. Finally, add these three keys to that dictionary, all using the String data type:
UIApplicationShortcutItemIconType
, value: UIApplicationShortcutIconTypeAdd
.UIApplicationShortcutItemTitle
, value: Add User
.UIApplicationShortcutItemType
, value: com.yoursite.yourapp.adduser
.You need all three of those keys, but you will want to change the values to whatever fits your needs.
The first one should be one of the built-in icon types, such as UIApplicationShortcutIconTypeCompose
, UIApplicationShortcutIconTypePlay
, UIApplicationShortcutIconTypeSearch
, UIApplicationShortcutIconTypeLove
, UIApplicationShortcutIconTypeShare
, or UIApplicationShortcutIconTypeAlarm
.
The second key should be the text string to show next to the shortcut icon. I've used "Add User" above, but you might want "Start Game", "Favorites", "New Message", and so on.
The third key should be a unique identifier, which is usually specified as your app's bundle ID followed by a new string. This is what identifies the command relative to other shortcuts you might add.
The shortcut item type is used when your app is launched using a shortcut menu item. The launchOptions
dictionary of didFinishLaunchingWithOptions
will have a key set called UIApplication.LaunchOptionsKey.shortcutItem
, which you can check to see what shortcut was triggered.
The code below – placed into your app delegate – will catch the shortcut we just created, although you should change the type string to match whatever you're using:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let shortcutItem = launchOptions?[UIApplication.LaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {
if shortcutItem.type == "com.yoursite.yourapp.adduser" {
// shortcut was triggered!
}
}
return true
}
If you want to create dynamic quick actions – which can live alongside static actions if you want - you need to create instances of UIApplicationShortcutIcon
and UIApplicationShortcutItem
, then assign to your application's shortcutItems
property like this:
let icon = UIApplicationShortcutIcon(type: .add)
let item = UIApplicationShortcutItem(type: "com.yoursite.yourapp.adduser", localizedTitle: "Add User", localizedSubtitle: "Meet someone new", icon: icon, userInfo: nil)
UIApplication.shared.shortcutItems = [item]
If your shortcut item should provide some sort of identifying information – perhaps it's the name of the most recently used contact – then you should place that into the userInfo
dictionary. This will then be provided back to you when the application gets launched, and you can respond appropriately.
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until September 29th.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Available from iOS 7.0
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.