Ios - Dynamically edit 3d touch shortcut list

I want to add a Continue shortcut to my game. But when the user completes my game completely, I want it to be either deleted or replaced with another shortcut. Is it possible? I know that 3d touch is handled by the ios system, but maybe there are some more options

+6
source share
4 answers

There are two ways to create shortcuts - dynamic and static.

  • Static are added to plist and not changed.
  • A speaker can be added and removed in code.

It looks like you need a dynamic shortcut, so something like this you will do:

To add:

if #available(iOS 9.0, *) { if (UIApplication.sharedApplication().shortcutItems?.filter({ $0.type == "com.app.myshortcut" }).first == nil) { UIApplication.sharedApplication().shortcutItems?.append(UIMutableApplicationShortcutItem(type: "com.app.myshortcut", localizedTitle: "Shortcut Title")) } } 

To delete:

 if #available(iOS 9.0, *) { if let shortcutItem = UIApplication.sharedApplication().shortcutItems?.filter({ $0.type == "com.app.myshortcut" }).first { let index = UIApplication.sharedApplication().shortcutItems?.indexOf(shortcutItem) UIApplication.sharedApplication().shortcutItems?.removeAtIndex(index!) } } 

Then you can process the shortcut by checking it in the application delegation method:

 @available(iOS 9.0, *) func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) { if shortcutItem.type == "com.app.myshortcut" { // Do something } } 

Be sure to check compatibility with iOS9 and 3D Touch.

Here you can find the Apple Developer 3D pages:

https://developer.apple.com/ios/3d-touch/

And especially the dynamic shortcuts here:

https://developer.apple.com/library/ios/samplecode/ApplicationShortcuts/Listings/ApplicationShortcuts_AppDelegate_swift.html#//apple_ref/doc/uid/TP40016545-ApplicationShortcuts_AppDelegate_swift-DontLinklement

+8
source

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) } } 
+3
source

I assume that you are talking about the so-called quick actions that the user can trigger by clicking on the icon of his application on his main screen. You can dynamically create and update them directly from your code. The best way to find out about all the features is with a code example.

0
source
  [UIApplication sharedApplication].shortcutItems = @[]; 

worked for me. another answer that suggests removing something from the array does not work as shortcutItems does not change.

0
source

All Articles