There is a convenient class here that starts disabling 3D-touch of your application. Of course, you can trigger any action, but this is probably the most common. Then it syncs when the application appears or goes to the background. I use this to call the My Projects section only after the user has generated it (VisualizerProject.currentProject.images.count> 0).
class AppShortcut : UIMutableApplicationShortcutItem { var segue:String init(type:String, title:String, icon:String, segue:String) { self.segue = segue let translatedTitle = NSLocalizedString(title, comment:title) let iconImage = UIApplicationShortcutIcon(templateImageName: icon) super.init(type: type, localizedTitle:translatedTitle, localizedSubtitle:nil, icon:iconImage, userInfo:nil) } } class AppShortcuts { static var shortcuts:[AppShortcut] = [] class func sync() { var newShortcuts:[AppShortcut] = [] //reverse order for display newShortcuts.append(AppShortcut(type: "find-color", title: "Find Color", icon:"ic_settings_black_24px", segue: "showColorFinder")) newShortcuts.append(AppShortcut(type: "samples", title: "Sample Rooms", icon:"ic_photo_black_24px", segue: "showSamples")) //conditionally add an item like this: if (VisualizerProject.currentProject.images.count > 0) { newShortcuts.append(AppShortcut(type: "projects", title: "My Projects", icon:"ic_settings_black_24px", segue: "showProjects")) } newShortcuts.append(AppShortcut(type: "visualizer", title: "Paint Visualizer", icon:"ic_photo_camera_black_24px", segue: "showPainter")) UIApplication.sharedApplication().shortcutItems = newShortcuts shortcuts = newShortcuts } class func performShortcut(window:UIWindow, shortcut:UIApplicationShortcutItem) { sync() if let shortcutItem = shortcuts.filter({ $0.type == shortcut.type}).first { if let rootNavigationViewController = window.rootViewController as? UINavigationController, let landingViewController = rootNavigationViewController.viewControllers.first { //Pop to root view controller so that approperiete segue can be performed rootNavigationViewController.popToRootViewControllerAnimated(false) landingViewController.performSegueWithIdentifier(shortcutItem.segue, sender: self) } } } }
Then in your application deletes add synchronization and make shortcut calls
func applicationDidEnterBackground(application: UIApplication) { AppShortcuts.sync() } func applicationDidBecomeActive(application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. AppShortcuts.sync() } @available(iOS 9.0, *) func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) { if let window = self.window { AppShortcuts.performShortcut(window, shortcut: shortcutItem) } }
source share