Confirming the intent status

After you've made sure that everything is in place to eventually handle the intent, you must confirm this to Siri. Every intent handler has a confirm method. The signature might vary, but there is always some form of confirmation. Refer to the documentation for the intent you are handling to confirm which method you are expected to implement. When you send messages, the confirmation method is confirm(sendMessage:completion:).

You can make the confirmation step as complex as you want. For example, you could check whether a message is too long, contains forbidden content, or virtually anything else. Most commonly, you'll want to make sure that the user is authenticated and allowed to send a message to the recipient.

Again, it's completely up to your best judgment to determine which conditions apply to your extension. The important takeaway for the confirm method is that you're expected to make sure that everything is in place to smoothly perform the action later.

Let's look at an example of a confirmation implementation to explore some of the possible outcomes of the confirmation stage:

func confirm(sendMessage intent: INSendMessageIntent, completion: @escaping (INSendMessageIntentResponse) -> Void) {

  guard let user = User.current(), user.isLoggedIn else {
    completion(INSendMessageIntentResponse(code: .failureRequiringAppLaunch, userActivity: nil))
    return
  }

  guard MessagingApi.isAvailable else {   
    completion(INSendMessageIntentResponse(code: .failureMessageServiceNotAvailable, userActivity: nil))
    return
  }

  completion(INSendMessageIntentResponse(code: .ready, userActivity: nil))
}

The preceding implementation checks whether a current user is available, and whether they are logged in or not. If no user exists, the intent handler will inform Siri that the app should be launched so that a user can log in.

Next, the availability of the API that will eventually handle the message sending is checked. Note that the user and api classes don't exist in the example project, and should be defined by you if you decide to go with this confirmation approach. These classes simply serve as placeholder examples to demonstrate how confirmation of an intent works.

If a user must be taken to your app in order to log in or for any other reason, Siri will automatically create a user activity that's passed to AppDelegate in your application. You must implement application(_:continue:restorationHandler:) to catch and continue the user activity.

A user activity that's created by Siri has its interaction property set. This property contains an INInteraction object that reflects the action that the user attempts to complete using Siri. A good implementation of application(_:continue:restorationHandler:) will fulfill this interaction as soon as possible inside of the app. It's also possible to create your own user activity if you want to add custom information that Siri doesn't pass on. If you want to do this, you should pass your custom user activity to the INSendMessageIntentResponse initializer.

Since the Hairdressers app doesn't use an external API and the user doesn't have to log in, the confirm(sendMessage:completion:) method does not have to be implemented. After ensuring that everything is in place, it's time to move on to the third stage of handling an intent: performing the desired action.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.125.139