Contents

handle(intent:completion:)

Handles the media playback request.

Declaration

func handle(intent: INPlayMediaIntent, completion: @escaping  @Sendable (INPlayMediaIntentResponse) -> Void)
func handle(intent: INPlayMediaIntent) async -> INPlayMediaIntentResponse

Parameters

  • intent:

    The Inplaymediaintent object that contains details about the user’s request. The Intents app extension confirms the information in this intent before the system calls the Handle(intent:completion:) method.

  • completion:

    The handler block to execute with your response. You must execute this handler while implementing this method. This handler has no return value and takes the following parameter:

    response

    The Inplaymediaintentresponse object you create that reports the status of the request. This parameter must not be nil.

Discussion

Because the lifespan for an Intents app extension is short, the INPlayMediaIntent handler tells the main app to start the media playback. For audio content, respond with the INPlayMediaIntentResponseCode.handleInApp response code. This code tells the system to launch the main app in the background and call application(_:handle:completionHandler:) on the UIApplicationDelegate object. Your app should begin the audio playback in this app delegate method.

The listing below tells the app to play the requested media in the background.

public func handle(intent: INPlayMediaIntent, completion: @escaping (INPlayMediaIntentResponse) -> Void) {
    let response = INPlayMediaIntentResponse(code: .handleInApp, userActivity: nil)
    completion(response)
}

For video content, respond with INPlayMediaIntentResponseCode.continueInApp. This code instructs the system to launch your app in the foreground and call application(_:continue:restorationHandler:) on the UIApplicationDelegate object. Your app should start the video playback in this app delegate method.

The listing below tells the app to play the requested media in the foreground.

public func handle(intent: INPlayMediaIntent, completion: @escaping (INPlayMediaIntentResponse) -> Void) {
    let response = INPlayMediaIntentResponse(code: .continueInApp, userActivity: nil)
    completion(response)
}