Instance Method

handlerForIntent:

Returns the object capable of handling the specified intent.

Required.

Declaration

- (id)handlerForIntent:(INIntent *)intent;

Parameters

intent

The intent object representing the request coming from Siri.

Return Value

The object capable of handling the specified intent or nil if your app does not handle the intent. The returned object should adopt the specific protocol for handling objects like the one in the intent parameter.

Discussion

This method acts as a dispatcher, mapping incoming intents to the object capable of handling them. In your implementation, check the type of the intent parameter and return an object capable of handling that type of intent. The object you return must adopt the protocol used to handle that intent. For example, when handling an INRequestRideIntent object, return an object that adopts the INRequestRideIntentHandling protocol.

The simplest way to implement this method is to check the class of the object in the intent parameter and return the appropriate object. Listing 1 shows an example of this method that creates custom objects to handle two ride-related intent objects. You are responsible for providing the handler objects and for ensuring that they adopt all of the necessary protocols to respond to a given intent. In this example, the MyRequestRideHandler class implements the INRequestRideIntentHandling protocol and the MyRideStatusHandler class implements the INGetRideStatusIntentHandling protocol.

Listing 1

Dispatching intents to specific handler objects

- (nullable id)handlerForIntent:(INIntent *)intent {
 
   // You can substitute other objects for self based on the specific intent
   if ([intent isKindOfClass:[INRequestRideIntent class]]) {
       return ([[MyRequestRideHandler alloc] init]);
   }
   else if ([intent isKindOfClass:[INGetRideStatusIntent class]]) {
       return ([[MyRideStatusHandler alloc] init]);
   }
   return nil;
}

When creating handler objects in this method, use the provided intent object only to determine which handler to create. Do not use the intent object to initialize your handler object and do not store a reference to the intent object for later use. SiriKit updates the intent object during the processing of the request to incorporate any new information provided by the user.

For information about how to handle intents, see SiriKit Programming Guide.