How to implement Siri Shortcuts into health iOS application 

Topics
Voice-plus
Author
Alessio Roberto
Publication Date
12 August 2019

How to implement Siri Shortcuts into health iOS application 

Apple introduced powerful voice functionality with iOS 12 and iOS 13: Siri Shortcuts. This new Siri function gives developers the opportunity to create connections inside your applications that Siri uses to improve the end user’s experience by being able to use application functionalities without needing to open the app.

For more background info, check my previous article about how Siri Shortcuts reduce the friction between users and applications.

About Siri Shortcuts

I won’t dig into the technical implementation here, but if you’re not yet familiar with Siri, check https://developer.apple.com/siri/ and https://developer.apple.com/documentation/sirikit

A nice example can be found here: https://developer.apple.com/documentation/sirikit/soup_chef_accelerating_app_interactions_with_shortcuts

I used these to develop my health application TakeURPill.

Siri Shortcuts have two flavours and each one has a specific use:

  • Activity
  • Intent

Activities are used to communicate to Siri when a user visualises a specific section of the application, so Siri can suggest this section in the future based on user behaviour. Through this suggestion the user can open a specific section of the application directly. The user can also create a custom voice command to open the section.

Intents are used to communicate to Siri when a user uses a specific function of the application. Like the Activity, Siri can suggest the intent and the user can handle the action without opening the application, also with a custom voice command.

In this article, I focus on Activities.

Before going in-depth, two important remarks:

  • Siri doesn’t open a specific section of the application by itself or runs a specific functionality, but only communicates to iOS to open the target application with a specific tag, so the application knows what to do.
  • Intent can handle a function without opening the application only if this Intent is marked as background intent by the developer.

 

Take Your Pill

TakeUrPill is a simple iOS application that helps you to keep track of the pills you took. It’s an open-source project which you can clone/fork from here.

It has only 3 functions:

  1. Set pill’s name and the amount you must take every time
  2. Register the pills you take on date and time
  3. View all the pills you took

Function #2 takes place within the homepage of the application, the other two happen in two different sections in the app.

 

Activity

A good use case for Activity in the TakeUrPill application is the History section. The user action related to this function is only to open and view a specific part of the application. So it would be great if the user can open the History right away by just asking Siri “Show my pills”.

The first step is to define an identity for the activity “the user opens the history view controller”. In the info.plist of your application, target add NSUserActivityTypes and add it to it a unique string to identify the activity, for example, com.mobiquityinc.demo.TakeUrPill.history:

 

1*ihnDLkG9vy_dK90cNWk_xw

(TakeURPill application info.plist)

Then you can create a specific service that handles the interaction between your presenter/view model and Siri. 

Screenshot 2019-08-09 at 10.03.06

At this point, we need to focus only on ActivityInformationstruct and activitySetupfunction.

The struct is an easy way to pass, the details from the Presenter to SiriService about any Activities we wants to share with Siri, like this:


let information = SiriService.ActivityInformation( activityType: "com.mobiquityinc.demo.TakeUrPill.history", activityTitle: NSLocalizedString("activity.history", comment: ""), activitySuggestedInvocation: NSLocalizedString("activity.history.suggestion", comment: ""))


activityType is the string we defined in the info.plist, activityTitle is the title that will be displayed to users. The last one is a suggestion we give to the user about the voice command the user can associate to the activity.

The activitySetup function returns a NSUserActivity with all the information present into the ActivityInformation, information that Siri needs to present and to handle this activity. It’s important to set the new NSUserActivity property isEligibleForPrediction as true, otherwise, your activity will be not passed (“donate” is the official Apple term) to Siri!


activity.isEligibleForPrediction = true


The following shows how to donate your activity to Siri, starting from the viewDidAppear of the UIViewController you want to track as Siri Shortcut Activity. Important note: 

 

Apple suggests setting the userActivity property of your UIViewController in the viewDidAppear function.

Screenshot 2019-08-09 at 10.06.32

Now, every time the user opens the History UIViewController, the application donates the com.mobiquityinc.demo.TakeUrPill.history activity to Siri. The last thing to create is the logic to show the History UIViewController to the user.

This implementation can change based on the architecture of your application. In this case, TakeURPill adopts MVP with a FlowController and these are the steps:

  • Implement in the AppDelegate the function application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool
  • Check the NSUserActivity, activityType and define a case for all the strings defined in the info.plist file
  • Trigger the logic to check the user state and show the History UIViewController. For demo purpose, I chose an easy implementation, with a Notification sent by the AppDelegate and observed by HomeViewController
  • The HomeViewController interacts with its Presenter and, if necessary, it communicates to its FlowController to start a new flow to the HistoryViewController

Screenshot 2019-08-09 at 10.07.301*8POme-t4NYNhQ13jBDt2GQ

(How the user can access directly the History of TakeUrPill from the Spotlight section)

 

I hope you found this useful! For more edigital and software news, follow us on social media or check out my blog @darthpelo

 

Let our expertise complements yours

We believe that addressing customer challenges gives you opportunities to delight. Using our proprietary Friction Reports and  strong industry expertise, we dig deep into customer sentiment and create action plans that remove engagement roadblocks. The end result is seamless, relevant experiences that your customers will love.