API Development

How to Add Siri Shortcuts to your Titanium App

One of the cool new features of iOS 12 is Siri Shortcuts, a way to enable Siri to interact with apps to perform specific tasks and actions configured within the app itself.

For example, in iOS 12 notes, you can associate a phrase with a note, so when I’m making home-made pizza I can say “Hey Siri, show my pizza recipe” and a specific note pops up with all my ingredients etc.

It’s a great feature and used in all kinds of ways by app developers, from retrieving specific notes from apps, finding friends and family or house keys, sending playing podcasts, loading web sites or opening up the “what’s happening” screen in Twitter.

Luckily, it’s easy to add this to your Titanium apps, using the same methods you may have already been familiar with if you’ve integrated your apps into Spotlight or have used Handoff.

Firstly, make sure your app’s provisioning profile has access to Siri, then add the following to the iOS > plist > dict section of the tiapp.xml file:

<key>NSUserActivityTypes</key>
<array>
<string>com.tipsApp.showView</string>
</array>

In this instance I’m registering a particular activity for use in my app. You might want to have activity types for different types of shortcuts, e.g. “showNote” or “createNote” etc.

Now we’ve done that, we need to add some code to the app itself to create a specific instance of an activity.

To keep this simple, I’m dropping this into the index.js file, but you could put it anywhere, wrap it in a module etc.

First things first, we need to register a new activity within our code:

var activity = Ti.App.iOS.createUserActivity({
    activityType: 'com.tipsApp.showView',
    title: 'Show my tasks for today',
    userInfo: {
      view: 'today'
    },
    eligibleForSearch: true,
    eligibleForPrediction: true,
    persistentIdentifier: 'tipsApp.showView.today'
  });

Here, we’re defining a new activity that matches the one we defined in the tiapp.xml file. It has a title and we’ve enabled eligibleForSearch so it will be found from Spotlight — we’ve also allowed Siri to suggest it as a shortcut and given it a unique identifier so we can delete it later.

Next, we need to check if this activity is supported on the device, and if so, we’re going to add the handler to pick up when the activity is run:

if (!activity.isSupported()) {
    alert('Sorry, user activities are not supported on this device!');
  } else {
    activity.becomeCurrent();
   
    Ti.App.iOS.addEventListener('continueactivity', function(e) {
      console.log('continueactivity called');
      if (e.activityType === 'com.tipsApp.showView' && e.userInfo.view) {
        alert('Show View: ' + e.userInfo.view);
      }
    });
  }

and that’s it. If we build and run the app, you can now use Spotlight to search and find the shortcut, or go to Settings > Siri > Shortcuts where you’ll see it suggested there.

Record a phrase, then use it and the app will launch and the activity will and you should see an alert message.

The example above is for a Todo app where you might have a “Today” view that you want to display, so we’re using a generic activityType called “showView”, then creating a specific userActivity based on that, and using the userInfo attribute to store a reference to a view. This could easily be an id of a todo item or anything else you want to reference.

In practice, I might want multiple activity types such as “showView” and “createTodo” or “showTodo” all of which could be passed different parameters via the userInfo to open specific screens; show specific todo items.

If you’re creating multiple activities in this way, it’s important to use the persistantIdentifier so you can reference a particular activity later if you need to delete it.

On that subject, and when it comes to cleaning up activities, there’s some additional methods that are useful here.

To delete all activities created for your app:

activity.deleteAllSavedUserActivities();

 

To delete a specific activity based on it persistent identifier:

activity.deleteSavedUserActivitiesForPersistentIdentifiers(['tipsApp.todo.showView.today']);

 

There’s also an event that’s fired when an activity is deleted:

activity.addEventListener('useractivitydeleted', function(e) {
console.log('useractivitydeleted called');
});

 

As you can see, it’s easy to add Siri Shortcuts to your Titanium app, allowing users to interact with its features with their voice.

You can find out more information, and browse the documentation on User Activity and Siri Shortcuts at Titanium.App.iOS.UserActivity

Let us know in the comments if you’re using Siri Shortcuts and how you’re using them in your apps.

Happy Coding!