Class

INUIAddVoiceShortcutButton

A button that allows the user to add or edit a shortcut.

Declaration

class INUIAddVoiceShortcutButton : UIButton

Overview

When the user performs an action such as placing an order for tomato soup, the app should provide the option to add the action to Siri as a shortcut. To present this option in your app, use INUIAddVoiceShortcutButton to display an “Add to Siri” button. Using this button makes your app consistent with other apps that support Siri Shortcuts.

Set the shortcut property on the button to have it automatically update the status of the shortcut. If the user has already added the shortcut to Siri, the button displays “Added” instead of “Add” and includes the phrase that the user chose when adding the shortcut. The methods in INUIAddVoiceShortcutButtonDelegate aren’t called unless the shortcut property is set.

After creating the button, assign its action to a method that displays INUIAddVoiceShortcutViewController. This controller guides the user through the process of adding the shortcut to Siri.

Listing 1

Add an “Add to Siri” button to a view and let the user record an invocation phrase

// Add an "Add to Siri" button to a view.
func addSiriButton(to view: UIView) {
    let button = INUIAddVoiceShortcutButton(style: .blackOutline)
    button.translatesAutoresizingMaskIntoConstraints = false

    view.addSubview(button)
    view.centerXAnchor.constraint(equalTo: button.centerXAnchor).isActive = true
    view.centerYAnchor.constraint(equalTo: button.centerYAnchor).isActive = true

    button.addTarget(self, action: #selector(addToSiri(_:)), for: .touchUpInside)
}

// Present the Add Shortcut view controller after the
// user taps the "Add to Siri" button.
@objc
func addToSiri(_ sender: Any) {
    if let shortcut = INShortcut(intent: orderSoupOfTheDayIntent) {
        let viewController = INUIAddVoiceShortcutViewController(shortcut: shortcut)
        viewController.modalPresentationStyle = .formSheet
        viewController.delegate = self // Object conforming to `INUIAddVoiceShortcutViewControllerDelegate`.
        present(viewController, animated: true, completion: nil)
    }
}

Topics

Creating a Shortcut Button

init(style: INUIAddVoiceShortcutButtonStyle)

Creates an Add to Siri button with the specified style.

enum INUIAddVoiceShortcutButtonStyle

The styles for the Add to Siri button.

Presenting Shortcut Editors

var shortcut: INShortcut?

The shortcut Siri invokes when the user speaks the invocation phrase.

var delegate: INUIAddVoiceShortcutButtonDelegate?

The object that receives presentation requests from the button.

protocol INUIAddVoiceShortcutButtonDelegate

The protocol an object implements to receive notifications from the Siri Shortcut button.