Running Other Apps' Siri Shortcuts Through Deep Links in Swift

October 18, 2018

Siri Shortcuts are definitely my favorite iOS 12 feature. Ever since SiriKit first came out I was very disappointed that you couldn't use it for your own custom commands, but the new Shortcuts app solves this problem. Not only it allows you to create your custom commands, but it also provides a very intuitive UI to allow even non-devs to automate tons of kinds of workflows in their iPhones.

We've all seen how to create a workflow and make your app expose custom actions, but one thing that I haven't seen people mention is that the Shortcuts app exposes several deep links for opening and running shortcuts - meaning that you can make an app that runs other apps' Siri Shortcuts. I used this to make an app run my "Take Picture" shortcut every time I tapped my AirPods.

The URL Scheme for the Shortcuts app is shortcuts://, and you can test all of them by typing them into Safari.

Running a specific Shortcut

shortcuts://run-shortcut?name=[name]&input=[input]

For this deep link, name is the URL-encoded name of the shortcut you're trying to run, and input is the string input of the shortcut, if applicable. (You can type clipboard as the input to use the clipboard's contents!)

To run my "Take Picture" shortcut, we just have to tell iOS to open this deep link:

let shortcut = URL(string: "shortcuts://run-shortcut?name=Take%20Picture")! UIApplication.shared.open(shortcut, options: [:], completionHandler: nil)

Returning to your app after a Shortcut ends

Running the shortcut is great, but you might have noticed that the deep link has to open the Shortcuts app in order to work. How can we return to our app after the shortcut ends?

Luckily for us, the Shortcuts app supports the x-callback-url standard, which is a standardized way for you to send data to other apps through URLs and get something back - in the shape of another URL. It looks like this:

shortcuts://x-callback-url/run-shortcut?name=[name]&x-success=[url]

When the [name] shortcut succeeds, the Shortcuts app will automatically try to open the URL passed to the x-success argument. This can be any url, and we can use this to return to our app by registering a URL scheme of our own. Assuming that our app is listening to a myapp:// URL scheme, we can make the Shortcuts app route back to our app by running:

let shortcut = URL(string: "shortcuts://x-callback-url/run-shortcut?name=Take%20Picture&x-success=myapp://")! UIApplication.shared.open(shortcut, options: [:], completionHandler: nil)

Besides x-success, x-cancel and x-error can be used to treat interruptions and errors, respectively.

Other Deep Links

Import a shortcut

shortcuts://import-shortcut?url=[url]&name=[name]

For this specific shortcut, url is the URL for a .shortcut file, and name is the name of the shortcut to be imported. You can also pass &silent=true to this deep link to prevent the shortcuts app from opening the imported shortcut.

This deep link also supports x-callback-url.

Open (not run) a specific shortcut

shortcuts://open-shortcut?name=[name]

Open the Shortcuts app

shortcuts://

Open the Create Shortcut screen

shortcuts://create-shortcut

Open the Shortcuts Gallery

shortcuts://gallery

Search the Shortcuts Gallery

shortcuts://gallery/search?query=[query]

Conclusion

Because there's no way for you to retrieve a list of the user's shortcuts, there are probably not many uses for these deep links in regular apps - but they can be very useful for personal automation projects like my AirPods camera trick. What are you planning to do?

Follow me on my Twitter - @rockbruno_, and let me know of any suggestions and corrections you want to share.

References and Good Reads