Siri Shortcuts with Xamarin

With iOS 12, Apple introduced Siri shortcuts. For us developers, we can create shortcuts using SiriKit. In this blog post, we will walk through how to do that in an app that we are building with Xamarin.iOS.

The first step is to update our provisioning profile.

    1. Sign in to https://developer.apple.com.
    2. Go to the account page
    3. Go to Certificates, Identifiers & Profiles
    4. Go to App IDs
    5. Select the App ID that you want to use and press the edit button.
    6. Enable the SiriKit service for the selected App ID.

 

    1. Go to publishing profiles and select the profile you want to use for your app that you will implement SiriKit in.
    2. Select to edit the publishing profile
    3. Regenerate the publishing profile
    4. Download the publishing profile and install it on the machine that you are using for building iOS app.

Now when we have the publishing profile we can open the app solution in Visual Studio to start to implement SiriKit. The first thing we will do is to add Siri to our Entitlements.plist file.

    

    
	com.apple.developer.siri
	
    

 

We need to verify that the Entitlements.plist is used as Custom Entitlements. We have to add the file for each configuration we want to use shortcuts for. Configurations for Custom Entitlements is one under the bundle signing page of the project settings.

We need to add all shortcuts that we will create to Info.plist. We will add them under the NSUserActivityTypes key. The nameing recommendation is {bundleID}.{activityName}. In the examle below there are shortcuts for sedning a message and one for reading unread messages.

NSUserActivityTypes
	
		se.danielhindrikes.DemoApp.sendMessage
		se.danielhindrikes.DemoApp.showUnreadMessages
	

Shortcuts are created in the ViewController that it should be shortcut to. The idea is that shortcuts should be created for pages in the apps that you use often.

If the apps have a minimum iOS version that is lower than 12, we need to verify that the user running the app on iOS 12 or higher.

if (UIDevice.CurrentDevice.CheckSystemVersion(12, 0))
{
    //Add code here
}

Next step is to create a UserActivity (shortcut). Here we creating a separate method for that.

public const string ViewMenuActivityType = "se.danielHindrikes.DemoApp.sendMessage";

private NSUserActivity GetSendMessageUserActivity()
{
      //Create a user activity and make it eligible for Siri to find for search and predictions.
      var userActivity = new NSUserActivity(ViewMenuActivityType)
      {
          Title = "Send message",
          EligibleForSearch = true,
          EligibleForPrediction = true
      };

      //Add som attributes to the user activity
      var attributes = new CSSearchableItemAttributeSet("SendMessage")
      {
          ThumbnailData = UIImage.FromBundle("Icon.png").AsPNG(),
          Keywords = "send, message, daniel, hindrikes",
          DisplayName = "Send message",
          ContentDescription = "Send message in the Daniel Hindrikes Demo App"
      };

      userActivity.ContentAttributeSet = attributes;
      
      //Add a suggestion for a phrase the user can use to activate the shortcut via Siri.
      var phrase = "Send message in the Daniel Hindrikes Demo App";
      userActivity.SuggestedInvocationPhrase = phrase;
      return userActivity;
}

When we have created the UserActivity we can use it to set the UserActivity property of the ViewController. We will do this in the ViewDidLoad override.

public override void ViewDidLoad()
{
     base.ViewDidLoad();

     if (UIDevice.CurrentDevice.CheckSystemVersion(12, 0))
     {
           UserActivity = GetSendMessageUserActivity();
           UserActivity.BecomeCurrent();
     }
}

If we are developing a Xamarin.Forms app, we can access the ViewController via a Custom Renderer for the page.

[assembly: ExportRenderer(typeof(SendMessageView), typeof(CustomSendMessageViewViewRenderer))]
namespace Se.Co.App.iOS.Renderers
{
    public class CustomSendMessageViewViewRenderer : PageRenderer
    {

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            if (UIDevice.CurrentDevice.CheckSystemVersion(12, 0))
            {
                 ViewController.UserActivity = GetShowStoresUserActivity();
                 ViewController.UserActivity.BecomeCurrent();
            }
        }

The last thing we need to do is to add code to the AppDelegate.cs that handles what should happen when a shortcut is activated.

public override bool ContinueUserActivity(UIApplication application, NSUserActivity userActivity, UIApplicationRestorationHandler completionHandler)
{
            if(userActivity.ActivityType == SendMessageViewController.ViewMenuActivityType)
            {
                //Navigate to the send message view.

                return true;
            }
            else if (userActivity.ActivityType == ShowUnreadMessagesViewController.ViewMenuActivityType)
            {
                //Navigate to the show unread messages view

                return true;
            }

            return base.ContinueUserActivity(application, userActivity, completionHandler);
}

To make it easier to test the shortcuts go to Settings and then to Developer and then enable, Display Recent Shortcuts and Display Donations on Lock Screen.

Now when you have deployed the app and run the views that added UserActivities, you will see shortcuts when you are swiping down at the home screen. You can also see all shortcuts if you open Siri under Settings.

Read more about how to use SiriKit in Xamarin.iOS here, https://docs.microsoft.com/en-us/xamarin/ios/platform/sirikit/

  10/16/2018 - 6:25 PM