read

Following up on the post on deeplink for UIScene, this post will be on handling 3D/Force touch shortcuts for UIScene. Again, because of UIScene, the old way of using AppDelegate will no longer work. So this post is a refresh.

We also had covered App Shortcuts, including setting up of dynamic shortcuts, so we won’t cover here.

1. Setup static shortcuts

In Info.plist, static shortcuts can be configured like this:

<key>UIApplicationShortcutItems</key>
<array>
  <dict>
    <key>UIApplicationShortcutItemIconType</key>
    <string>UIApplicationShortcutIconTypeCaptureVideo</string>
    <key>UIApplicationShortcutItemTitle</key>
    <string>Record Video</string>
    <key>UIApplicationShortcutItemType</key>
    <string>recordVideo</string>
  </dict>
</array>

The UIApplicationShortcutItemType is like an identifier which will be used in code later.

For more information on setting up app shortcuts, refer to Apple’s doc.

2. Handle when app is running

Implement in SceneDelegate the protocol UIWindowSceneDelegate. Our custom method handleShortcut() will identify which shortcut it is and handle accordingly.

func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
    completionHandler(handleShortcut(shortcutItem))
}

@discardableResult private func handleShortcut(_ shortcutItem: UIApplicationShortcutItem) -> Bool {
    switch shortcutItem.type  {
    case "recordVideo":
        // Handle this
    default: return false
    }
    return true
}

3. Handle cold start

As for cold start, you will need to handle in scene(_:willConnectTo:options:) instead.

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Your typical window setup, or even handleDeepLink..

    if let shortcutItem = connectionOptions.shortcutItem {
        handleShortcut(shortcutItem)
    }
}

Image

@samwize

¯\_(ツ)_/¯

Back to Home