Article

Confirming the Details of an Intent

Perform final validation of the intent parameters and verify that your services are ready to fulfill the intent.

Overview

After you resolve the parameters of an intent, but before you handle that intent, SiriKit may ask you to confirm the intent details. The confirmation phase is your chance to perform any final validation of the intent parameters and to verify that any needed services are available. For example, you might confirm that you can communicate with your company’s server. After performing your final validation, you provide SiriKit with a response object indicating how you plan to handle the request.

Listing 1 shows a confirmation method used by a running app to validate an INStartWorkoutIntent object. Because the app uses Core Location to map the user’s run, the method verifies that the needed location services are available and returns a failure code if they are not. Otherwise, the response object contains the INStartWorkoutIntentResponseCode.ready code, which indicates that the app is ready to start the run.

Listing 1

Confirming the start of a workout

func confirm(startWorkout intent: INStartWorkoutIntent, 
       completion: @escaping (INStartWorkoutIntentResponse) -> Void) {
    var code : INStartWorkoutIntentResponseCode = .ready
    var activity : NSUserActivity? = nil
        
    // If location services are not available, the user
    // might need to change the type of workout.
    if !CLLocationManager.locationServicesEnabled() {
        code = .failureRequiringAppLaunch
        activity = NSUserActivity(activityType: 
              "com.example.myRunningApp.noLocationAvailable")
    }
        
    // Deliver the response back to SiriKit.
    let response = INStartWorkoutIntentResponse(code: code, 
            userActivity: activity)
    completion(response)
}

Providing a custom NSUserActivity object with your response during confirmation is recommended but not required. If SiriKit needs to launch your app for any reason, it passes the provided activity object to your app. Your app can use any information in the object to take further actions. For example, you might include custom data in the user activity object that your app can then use to configure its interface.

See Also

Resolving and Handling Intents

Resolving the Parameters of an Intent

Validate the parameters of an intent and make sure that you have the information you need to continue.

Handling an Intent

Fulfill the intent and provide feedback to SiriKit about what you did.