NEW! Master Swift design patterns with my latest book! >>

How to add a UIApplicationShortcutItem quick action for 3D Touch

Paul Hudson       @twostraws

There are two ways to add a shortcut item for 3D Touch: you can register a list of static items that always get shown, or you can create a dynamic list in code based on user information.

Let's start by tackling static lists. Open your Info.plist file, and add a new key called UIApplicationShortcutItems, then set it to be an Array. Add one new item in there, which will get the name "Item 0", and set its data type to be Dictionary. Finally, add these three keys to that dictionary, all using the String data type:

  • Key name: UIApplicationShortcutItemIconType, value: UIApplicationShortcutIconTypeAdd.
  • Key name: UIApplicationShortcutItemTitle, value: Add User.
  • Key name: UIApplicationShortcutItemType, value: com.yoursite.yourapp.adduser.

You need all three of those keys, but you will want to change the values to whatever fits your needs.

The first one should be one of the built-in icon types, such as UIApplicationShortcutIconTypeCompose, UIApplicationShortcutIconTypePlay, UIApplicationShortcutIconTypeSearch, UIApplicationShortcutIconTypeLove, UIApplicationShortcutIconTypeShare, or UIApplicationShortcutIconTypeAlarm.

The second key should be the text string to show next to the shortcut icon. I've used "Add User" above, but you might want "Start Game", "Favorites", "New Message", and so on.

The third key should be a unique identifier, which is usually specified as your app's bundle ID followed by a new string. This is what identifies the command relative to other shortcuts you might add.

The shortcut item type is used when your app is launched using a shortcut menu item. The launchOptions dictionary of didFinishLaunchingWithOptions will have a key set called UIApplicationLaunchOptionsKey.shortcutItem, which you can check to see what shortcut was triggered.

The code below – placed into your app delegate – will catch the shortcut we just created, although you should change the type string to match whatever you're using:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {
        if shortcutItem.type == "com.yoursite.yourapp.adduser" {
            // shortcut was triggered!
        }
    }

    return true
}

If you want to create dynamic quick actions – which can live alongside static actions if you want - you need to create instances of UIApplicationShortcutIcon and UIApplicationShortcutItem, then assign to your application's shortcutItems property like this:

let icon = UIApplicationShortcutIcon(type: .add)
let item = UIApplicationShortcutItem(type: "com.yoursite.yourapp.adduser", localizedTitle: "Add User", localizedSubtitle: "Meet someone new", icon: icon, userInfo: nil)
UIApplication.shared.shortcutItems = [item]

If your shortcut item should provide some sort of identifying information – perhaps it's the name of the most recently used contact – then you should place that into the userInfo dictionary. This will then be provided back to you when the application gets launched, and you can respond appropriately.

Available from iOS 7.0

Did this solution work for you? Please pass it on!

Other people are reading…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Want to build macOS apps?

Hacking with macOS delivers 18 awesome projects that teach you macOS development in no time – take control of your desktop today!

MASTER SWIFT NOW
Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns Buy Practical iOS 11 Buy Swift Coding Challenges Buy Server-Side Swift (Vapor Edition) Buy Server-Side Swift (Kitura Edition) Buy Hacking with macOS Buy Advanced iOS Volume One Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with Swift Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let me know!

Click here to visit the Hacking with Swift store >>