Home Screen Quick Actions for iOS: Getting Started

Learn how to integrate Static and Dynamic Home Screen Quick Actions into your SwiftUI iOS app. By Felipe Laso-Marsetti.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Adding Your Dynamic Quick Actions

As mentioned earlier, a good place to add your dynamic actions is when the app goes into the background. At this point, you have all the information needed to run the code that determines what actions to add and then add them.

Since you’re already observing the ScenePhase in NoteList, that’s a good place to update the actions. But before you do that, you need to create your own instances of UIApplicationShortcutItem that describe your dynamic action.

Head to Note.swift and add the following import at the top of the file:

import UIKit

Inside the Note extension, add the following property:

// 1
var shortcutItem: UIApplicationShortcutItem? {
  // 2
  guard !wrappedTitle.isEmpty || !wrappedBody.isEmpty else { return nil }

  // 3
  return UIApplicationShortcutItem(
    type: ActionType.editNote.rawValue,
    localizedTitle: "Edit Note",
    localizedSubtitle: wrappedTitle.isEmpty ? wrappedBody : wrappedTitle,
    icon: .init(systemImageName: isFavorite ? "star" : "pencil"),
    userInfo: [
      "NoteID": identifier as NSString
    ]
  )
}

Here’s a code breakdown:

  • Use the type defined by ActionType so it matches the expected value when you read it back later.
  • Provide a user-friendly title, like you did in Info.plist.
  • To personalize the action some more, you include either the title or body of the note as the subtitle of the quick action.
  • Use an appropriate SF Symbol to represent if the item is a favorite or not.
  • Include the unique identifier of the Note in the userInfo so that you’ll know which note to edit when you respond to the quick action.
  1. You define a new computed property on Note called shortcutItem. The property is optional because you don’t need to represent every note as a quick action.
  2. If the note doesn’t have a title or body, you return nil so that this note won’t be represented.
  3. Then you initialize a new instance of UIApplicationShortcutItem and return it.

Now that you created a property to expose the appropriate shortcut for a given Note, navigate back to NoteList.swift. Add the following method beneath performActionIfNeeded():

func updateShortcutItems() {
  UIApplication.shared.shortcutItems = notes.compactMap(\.shortcutItem)
}

Here you compact map the notes array to produce an array of just the non-nil UIApplicationShortcutItem‘s. This array is then assigned to the UIApplication‘s shortcutItems property which, in turn, becomes available alongside your static actions in the Home Screen Menu.

Now, you just need to call this method whenever the app goes into the background. Scroll back up to the onChange(of: perform:) modifier that you added earlier and add the following inside of the switch statement:

case .background:
  updateShortcutItems()

Build and run. Then have a look to see which items are available:


Animated clip of the Quick Action launching Note Buddy and creating a new note

Yay! You’re done.

Grey smiley face emoticon.

Make some edits to your notes, favorite a few and repeat the process. The notes shown, as well as the order, stay perfectly in sync whenever you background the app:


Animated clip demonstrating how Quick Actions are updated to reflect in-app changes

You may have noticed that not all of your notes show up. iOS limits the number of actions to only show those that fit on-screen. However, the documentation suggests you don’t specifically limit the count of dynamic actions. Instead, do it as you have for Note Buddy.

With that, iOS will show all the quick actions it can fit. If future updates enable more, that’s fantastic as you don’t need to push out an app update to support adding more actions before you background the app.

Where to Go From Here?

Download the finished project by clicking Download Materials at the top or bottom of this tutorial.

And there you have it: You’re equipped and ready to add quick actions to your apps and projects.

If you’d like to know more, take a look at the Apple documentation for Menus and Shortcuts in UIKit, as well as the Human Interface Guidelines for Home Screen Quick Actions.

What other cool uses of quick actions can you think of? Thanks for following along. Leave your implementations and ideas in the comments.