GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

How to add a UIApplicationShortcutItem quick action for 3D Touch

Swift version: 5.10

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 UIApplication.LaunchOptionsKey.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: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    if let shortcutItem = launchOptions?[UIApplication.LaunchOptionsKey.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.

Hacking with Swift is sponsored by try! Swift Tokyo.

SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!

Get your ticket here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Available from iOS 7.0

Similar solutions…

About the Swift Knowledge Base

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

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Interview Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.0/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.