Convert SiriKit Custom Intents using non configurable parameters to AppIntents

2 min read 04-10-2024
Convert SiriKit Custom Intents using non configurable parameters to AppIntents


Migrating SiriKit Custom Intents with Non-Configurable Parameters to AppIntents

Problem: You've built a SiriKit integration using custom intents with parameters that are not configurable by users. Now you're looking to migrate to AppIntents for a smoother development process and improved user experience. However, you're unsure how to handle these non-configurable parameters.

Rephrased: Imagine your app lets users tell Siri to "Play my favorite playlist." The "favorite playlist" part isn't something the user can change through Siri; it's pre-defined within your app. How do you transition this functionality to AppIntents without needing to configure these fixed parameters within the intent?

Scenario:

Let's assume you have a SiriKit custom intent called PlayPlaylistIntent with a parameter named playlistName that represents the name of the playlist to play. This playlistName is always predefined in your app and doesn't allow user customization through Siri.

Original Code:

// SiriKit custom intent
class PlayPlaylistIntent: INIntent {
    var playlistName: String? // Non-configurable parameter
}

// Intent handler
class PlayPlaylistIntentHandler: NSObject, INPlayPlaylistIntentHandling {
    func handle(playPlaylist intent: PlayPlaylistIntent, completion: @escaping (INPlayPlaylistIntentResponse) -> Void) {
        // Access the predefined playlistName and play the corresponding playlist
        if let playlistName = intent.playlistName {
            // Play playlist with name 'playlistName'
        } else {
            // Handle error if playlistName is missing
        }
    }
}

Moving to AppIntents:

AppIntents simplify the development process by offering a more structured and flexible approach to intent handling. However, non-configurable parameters within AppIntents require a different strategy.

Solution:

Instead of directly passing the non-configurable parameter as an intent parameter, you can:

  1. Embed it within the intent's identifier:

    • Create a unique identifier for your intent that incorporates the non-configurable parameter. For instance, you could use playPlaylist_Favorites instead of simply playPlaylist.
    • Pass this identifier to the AppIntent structure when launching the intent.
  2. Handle the intent based on the identifier:

    • In your app's intent handling logic, extract the non-configurable parameter from the intent identifier.
    • Use this extracted information to identify the correct action to perform.

Example:

// AppIntent Identifier
let playPlaylistFavoritesIntentIdentifier = "playPlaylist_Favorites" 

// Intent handling in your app
func handleAppIntent(_ intent: AppIntent) {
    if intent.identifier == playPlaylistFavoritesIntentIdentifier {
        // Play the predefined 'Favorites' playlist
    } else if intent.identifier == "playPlaylist_Recent" {
        // Play the predefined 'Recent' playlist
    } 
}

// Launch intent
let intent = AppIntent(identifier: playPlaylistFavoritesIntentIdentifier)
// ... launch the intent

Benefits:

  • Clearer intent structure: You maintain a structured intent system while accommodating non-configurable parameters.
  • Simplified development: AppIntents' streamlined approach makes development and testing easier.
  • Enhanced user experience: Users can still interact with Siri naturally without needing to configure fixed parameters.

Conclusion:

By strategically incorporating non-configurable parameters within the intent identifier, you can successfully migrate from SiriKit custom intents to AppIntents while maintaining the functionality of your app's Siri integration. This approach ensures a smooth transition and an improved user experience.