Interacting with Siri on Xamarin in iOS 10

Introduction

In iOS9 you can not do any development or integration with Siri, it is very closed. However with NSUserActivity or CoreSpotlight, since Siri accesses the same search engine, it can launch your app if your app appears in the search results brought up by Siri, but it’s very limited and you have no ability to configure it.

iOS 10 finally allows third-parties to integrate with Siri, however it is still limited. Siri integration works by having Domains and Intents, however these Domain’s and Intent’s are limited. At the moment the only Domains you can work with are:

  1. Messaging (think WeChat)
  2. Ride Booking (think Uber)
  3. Photo Search
  4. Payments
  5. VOIP Calling
  6. Workouts
  7. Climate and Radio (specifically for CarPlay)
  8. (in the documentation they also mention for Maps only) Restaurant Reservations.

If your app doesn’t fit into any of these domains, then your app won’t integrate with Siri. However as with other new feature releases from Apple, I suspect these groups will expand over time.

Getting Started

Pre-Release Warning. I am doing all of this on Preview versions of Xamarin along with Xcode Beta 4. It may change, it will certainly become easier and I will update this blog post once the final release comes out in September.

First you need to have iOS 10 SDK’s that involves XCode 8, which is currently in beta. Xamarin already has the early preview of iOS 10 which includes SiriKit, which will be available in the alpha channel.

Set Entitlements

  1. Go to developer.apple.com
  2. Edit your App ID to enable SiriKit
  3. You will need to regenerate and download new provisioning profiles if you are modifying an existing App Identifier.

SiriKitCapability

Create Extension

Next you need to create the Intents Extension. In the future you will go and create a new project, select extensions under iOS and there will be Intents Extension. Xamarin Preview does not seem to have this yet. Hence below is an image of the Xcode selection as a placeholder until I can get a Xamarin one.

IntentsExtension

What I did is converted the Intents Extension over to C# in my SiriKitTest sample project, if you want to see the code that it generates.

Intents Supported

If you go to your info.plist you will see that it shows the intents supported. By default mine added INSendMessageIntent. These are predefined intents and you can add more to the list. For a list of supported intents have a look at Intent Domains.

IntentsSupported

Handling Intent

Error: (Update: This was verified and fixed, I now just need to find some time to update this post and test the code. Coming Soon) I believe there is a bug in Xamarin and the API’s weren’t generated correctly. This code will not work and waiting on a response to proceed. Why did you post this blog then? I am finishing off my series and it was blocking 🙂

In your extension create an IntentHandler as below.

public class IntentHandler: INExtension, IINSendMessageIntentHandling
{
   public IntentHandler() {}

   public void HandleSendMessage(INSendMessageIntent intent, Action<INSendMessageIntentResponse> completion)
   {
      var userActivity = new NSUserActivity(activityType: "INSendMessageIntent");

      var response = new INSendMessageIntentResponse(code: success, userActivity: userActivity);

      completion(response);
   }

   // Issue: no overrides for ResolveContent
   public override ResolveContent(INSendMessageIntent intent, Action<INStringResolutionResult> completion)
   {
       if (intent.Content != "")
       {
           completion(INStringResolutionResult.GetSuccess(intent.Content));
       }
       else {
          // Issue: NeedsValue returns the base type and not derived type.
          completion(INStringResolutionResult.NeedsValue);
       }
    }
 }

For more information you might want to look at Introduction to SiriKit by Apple or Understanding SiriKit Concepts by Xamarin. I will revisit this post once the final release comes out.


Posted

in

by

Tags: