UPDATED for Swift 4.2. And for UIScene
, refer to this.
With 3D Touch (or Force Touch), Apple adds a new way of interacting with your app.
One of the new feature is having app shortcuts when you 3D touch the app icon.
Type of shortcuts
- Static - declared in
Info.plist
- Dynamic - created with UIMutableApplicationShortcutItem
You can have up to 4 shortcuts.
Creating Static Shortcut Item
You add a UIApplicationShortcutItems array to Info.plist
, like this:
The title and subtitle strings are localized in InfoPlist.strings
.
You can provide your own icon with UIApplicationShortcutItemIconFile
. If you want to use system provided icons, then the key is UIApplicationShortcutItemIconType
eg. UIApplicationShortcutIconTypeTaskCompleted
.
Creating Dynamic Shortcut Item
You create 1 or more UIMutableApplicationShortcutItem
, and set it to UIApplication.sharedApplication().shortcutItems
.
// Creating a shortcut
let shortcut1 = UIMutableApplicationShortcutItem(
type: "com.myapp.shortcut1",
localizedTitle: "Shortcut 1",
localizedSubtitle: nil,
icon: UIApplicationShortcutIcon(templateImageName: "myimage"),
userInfo: ["foo": "bar" as NSString])
// Set the shortcut items
UIApplication.sharedApplication().shortcutItems = [shortcut1, shortcut2]
Note: You can have up to 4 shortcuts, and the static shortcuts will be listed first.
In the example above, I used a custom icon image “myimage”. You can easily use system provided icons such as .search
.
You can pass more information via userInfo
.
Handling a Shortcut
Unsurprisingly, you handle in your UIApplicationDelegate
.
// When app is ALREADY launched, performActionForShortcutItem will be called
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
completionHandler(handleShortcut(shortcutItem))
}
// When app is launched as a result of the shortcut, `didFinishLaunchingWithOptions` will be called
// If `handleShortcut` somehow returned false, `performActionForShortcutItem` will be called subsequently.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
if let shortcutItem = launchOptions?[.shortcutItem] as? UIApplicationShortcutItem {
return handleShortcut(shortcutItem)
}
return true
}
// Our private handler
private func handleShortcut(_ shortcutItem: UIApplicationShortcutItem) -> Bool {
switch shortcutItem.type {
case "com.myapp.shortcut1":
// Handle shortcut 1
case "com.myapp.shortcut2":
// Handle shortcut 2
default:
return false
}
return true
}
From the UIApplicationShortcutItem
, look for the type
of shortcut. You can get more details from the userInfo
.
That’s it!
Bonus: 3D Touch on Simulator
You can perform 3D Touch on simulator if you have a trackpad that supports Force Touch.
Go to Hardware > Use Trackpad Force for 3D Touch.
Tap lightly on the trackpad, and the quick actions shortcuts will be shown.
Bonus: Peek and Pop
With 3D Touch, another feature you can implement is previewing a view controller.
Krakendev has a comprehensive tutorial on that.