Launching with UIApplicationShortcutItem Launching with UIApplicationShortcutItem ios ios

Launching with UIApplicationShortcutItem


  1. In Xcode, open Product -> Schemes -> Edit Schemes
  2. In your Run Scheme, change the Launch setting to 'Wait for executable to be launched'

Now, if you turn on debugging and run your app, Xcode will wait for you to launch your app from the home screen so you are able to test launching it using a 3D Touch Shortcut Item.

See Screenshot in Xcode of the Setting


I finally got this working. Here's what my AppDelegate.swift file ended up as;

class AppDelegate: UIResponder, UIApplicationDelegate {// Propertiesvar window: UIWindow?var launchedShortcutItem: UIApplicationShortcutItem?func applicationDidBecomeActive(application: UIApplication) {    guard let shortcut = launchedShortcutItem else { return }    handleShortcut(shortcut)    launchedShortcutItem = nil}func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {    // Override point for customization after application launch.    var shouldPerformAdditionalDelegateHandling = true    // If a shortcut was launched, display its information and take the appropriate action    if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem {        launchedShortcutItem = shortcutItem        // This will block "performActionForShortcutItem:completionHandler" from being called.        shouldPerformAdditionalDelegateHandling = false    }    return shouldPerformAdditionalDelegateHandling}func handleShortcut( shortcutItem:UIApplicationShortcutItem ) -> Bool {    // Construct an alert using the details of the shortcut used to open the application.    let alertController = UIAlertController(title: "Shortcut Handled", message: "\"\(shortcutItem.localizedTitle)\"", preferredStyle: .Alert)    let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil)    alertController.addAction(okAction)    // Display an alert indicating the shortcut selected from the home screen.    window!.rootViewController?.presentViewController(alertController, animated: true, completion: nil)    return handled}func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {    completionHandler(handleShortcut(shortcutItem))}

Much of this was taken from Apple's sample code for UIApplicationShortcuts, and while I'm having my app launch an alert to prove that it is recognizing the proper shortcut was chosen, this could be adapted to your code to pop the view controller.

I think the func applicationDidBecomeActive was the critical part that I was missing, and removing the self.handleShortCut(shortcutItem) from didFinishLaunchingWithOptions (otherwise it was calling handleShortCut twice, it seemed).


For Swift 4.2

   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {    // Override point for customization after application launch.    var isLaunchedFromQuickAction = false    if let shortcutItem = launchOptions?[UIApplication.LaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {        isLaunchedFromQuickAction = true        handleQuickAction(shortcutItem: shortcutItem)    }    return isLaunchedFromQuickAction}


matomo