Contents

Dispatching intents to handlers

Provide SiriKit with an intent handler capable of handling a specific intent.

Overview

When a user makes a request of your app as an intent, SiriKit needs a handler that conforms to the corresponding intent handling protocol. Each intent object has an associated protocol based on the name of the intent. For example, to handle an INSendMessageIntent, provide an instance of a type that conforms to the INSendMessageIntentHandling protocol. The protocol defines the methods that your handler implements to resolve any intent parameters and to let SiriKit know how your app handled the intent.

Provide a handler in your intents app extension

An intents app extension helps you respond to a person’s request quickly, without loading the entire app. For intents you support in an extension, SiriKit loads your intents app extension and creates an instance of your INExtension subclass. Implement the handler(for:) method on your extension object to provide SiriKit with the handlers that you use to handle specific intents.

Provide a handler in your app

If you support an intent directly in your app, the system asks your app delegate for the handler. In a macOS app that uses AppKit, implement application(_:handlerFor:) on your NSApplicationDelegate. In an iOS app (or an app built with Mac Catalyst), implement application(_:handlerFor:) on your UIApplicationDelegate. If you’re using SwiftUI, use UIApplicationDelegateAdaptor or NSApplicationDelegateAdaptor to include an app delegate in your app.

Determine which handler type to return

You can define a separate type for handling each intent class to keep your code organized. When the system asks your app delegate or Intents app extension for an intent handler, check the type of the intent you receive to determine which type of handler to return. Return a valid new handler that conforms to the relevant protocol for all intents that your app or extension supports.

The following code listing shows logic for an app or extension that supports four different intents. The code handles three message intents with one class, MyMessageHandler, which implements INSendMessageIntentHandling, INSearchForMessagesIntentHandling, and INSetMessageAttributeIntentHandling. The code provides a separate handler, MyCallHandler, which implements INStartCallIntentHandling. After checking the type of the provided intent object, the method creates and returns an instance of the type capable of handling that intent.

// Inside your implementation of handler(for:) or application(_:handlerFor:), 
// use the type of the intent to determine which handler to provide.
switch intent {
    case is INSendMessageIntent,
            is INSearchForMessagesIntent,
            is INSetMessageAttributeIntent:
        return MyMessageHandler()

    case is INStartCallIntent:
        return MyCallHandler()
    default:
        // SiriKit doesn't call this method with intents you don't support.
        return nil
}

Handle an intent in multiple versions of iOS

In iOS 14 and later, you can resolve, confirm, and handle long-running or memory-intensive intents directly in your app. In iOS 13 and earlier, SiriKit required you resolve and confirm all intents in an Intents app extension, then send long-running intents to your app for the final handling step. To continue supporting these intents on earlier versions of iOS, you can implement the methods to resolve and confirm the intent in both your app delegate and Intents app extension. When your extension confirms the intent, respond with a continueInApp or handleInApp response code so the system sends the intent to the app to handle the intent.

See Also

Articles