How to add Siri shortcuts in your application?

Tram Ho

With the release of iOS 12, Apple allowed developers to create custom purposes that could be used with Siri. This new feature gives developers the ability to create custom actions within apps that can be activated with Siri without opening the app. Today in this tutorial, you will learn how to use a simple shortcut key to perform an action when called with Siri.

Start!

First, open Xcode and create a new project. Select Single View Application

Go to Target Settings, Signing & Capabilities and add Siri.

Now, right-click on the project folder and click on New File. Then search for the intent keyword and click next to create SiriKit Intent Definition File, which we will use in the next steps.

We start creating a new intent. Select File intents, click + at the bottom of the screen and then select New Intent.

Now reset the Title and Description for the intent and save.

Open ViewController and add the following code:

Now, if you run the application, you should have a blank screen with a button in the middle to add the shortcut you just created for Siri. You can press the button and use the default text or put a new button to activate the shortcut. Now if you activate Siri and speak the selected command, your application will automatically open!

Now your app will open when you speak commands to Siri, but what do you have to do to associate this phrase with a specific action in your application? First, go to Main.storyboard and set ViewController for the StoryboardID property.

Now on SceneDelegate.swift (on iOS 13) or on AppDelegate.swift (before iOS 13), copy the following code (this is the version for SceneDelegate, but on AppDelegate, the method to add the same):

Check it out

Finally, you are ready to test your application! Let Siri know your command and this will be the result:

The article is referenced here .

Share the news now

Source : Viblo