Implementing a UIApplicationShortcutItem

272 Views Asked by At

I was looking at the UIKit framework reference and came across UIApplicationShortcutItem. It is for 3D Touch.

It contains this code sample:

let existingShortcutItems = UIApplication.sharedApplication().shortcutItems ?? []
let anExistingShortcutItem = existingShortcutItems[anIndex]
var updatedShortcutItems = existingShortcutItems
let aMutableShortcutItem = anExistingShortcutItem.mutableCopy() as! UIMutableApplicationShortcutItem
aMutableShortcutItem.localizedTitle = “New Title"
updatedShortcutItems[anIndex] = aMutableShortcutItem
UIApplication.sharedApplication().shortcutItems = updatedShortcutItemsode here

If I am correct you put this in the app delegate?

And how does this work? Is the localizedTitle method called to check the title of the action selected, and then call the action that has that title? Where are the actions held?

1

There are 1 best solutions below

0
On

The code you've supplied will allow you to create shortcut items (i.e. the UI associated with short cuts that are revealed on the home screen when a user 3D-touches an app icon). To respond to those shortcut items you will need to implement:

- (void)application:(UIApplication *)application
performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem
  completionHandler:(void (^)(BOOL succeeded))completionHandler

or

optional func application( application: UIApplication,
performActionForShortcutItem shortcutItem: UIApplicationShortcutItem,
           completionHandler completionHandler: (Bool) -> Void)

in your UIApplicationDelegate. You can read about it in the UIApplicationDelegate Protocol Reference Guide. But the gist is this method will be called when your app is launched from a shortcut and you can inspect the shortcutItem to perform the appropriate action.