MindInventory
Integrating Siri

The Entire Process of Integrating Siri in the Third-Party Apps

Ever since Apple introduced its voice recognition assistant in 2011, the Artificial Intelligence (AI) based technology is going great guns. Following its successful launch, there were further enhancements and the WWDC Apple Event in 2017 saw the vital announcement of Siri integration with the third-party apps.

However, this integration was limited to a few apps only such as messaging, payments, photo search, and booking apps. With the release of iOS 11, this list was further extended and one also witnessed the introduction of SiriKit.

The introduction of SiriKit has added another feather into the cap. This framework has simplified the task of the iOS app developers to a great extent. It has become much easier to add new customized features to Siri, which is then integrated with the third-party apps. But it must be noted that you can get access to the benefits of SiriKit only if you are using iOS 10 or the advanced versions.

One of the major factors that have led to the increment of the popularity of Siri is that the users’ just need to give a voice command and the task is performed. So, whether it is paying a monthly electric bill, or adding a new item into your online shopping cart, use voice command to get the work done.

If we talk about the stat and figures, then according to Business Insider, about 70% of the iPhone users agree that they have used Siri sometimes or rarely. On the other hand, Statista, reports that 51% of Americans comprehensively and clearly understand the usage of Siri in their devices.

In fact, the addition of functionality has indeed become easier as it needs to be implemented with the help of SiriKit.

Here we would have a detailed discussion on how to integrate the Siri with a third-party app.

Selecting a Particular Niche

First things first, you need to choose a particular domain or niche or the app based on the app activity. However, it must be noted that SiriKit does not support all app domains; so, you have limited choice. For example, you can choose messaging apps, payment, workouts, image searching, climate and radio etc. You can find more options from the SiriKit Programming Guide.

Siri makes use of the intents to perform the tasks. When the user wants Siri to fulfil a task, it would send the user intent with other details to the handler of that intent. The intent comprise of its own protocol, which the manager incorporates for that intent. You also get the advantage of expanding the default User Interface in the Siri window.

Building a Cocoa Touch Framework

Once you have chosen the app domain, the next task is to build a Cocoa Touch framework for the application. You should remember to receive the Siri support through the integration of the Intents Extension and that you cannot use the codes directly.

Therefore, you need to take the help of the Cocoa Touch framework. For this, you have to first visit the Xcode menu and then choose File–>New–>Target. This will set up the Cocoa Touch Framework.

In the next step, you would be asked to choose the template that you want to use for the target. Then you would get across a window where you can change the template option and name your framework. You need to verify carefully that the framework contains the similar project and check the insert in application field.

The Addition of Classes to Framework

Upon the completion of the above task, the next job in hand is adding the classes to the framework. You can add the classes, which highlight your domain of the user request sending the requests for the backend development process.

In case, your class is already integrated move on to the name of your framework. Now, you need to eliminate the class from the key target and add them manually to the framework target.

The Integration of the Intent Handler

Now we have reached the most crucial step of integrating the Siri in third-party apps. Here we have to implement the intent handler. But before you go into the details, the first job in hand is to add an intent extension target to your app.

You can do this by the following process- File–>New–>Target. Next choose the Intents Extension. At this stage you will be required to fill out a name such as the SiriIntentExtension.”

Now you also have to add a few data to the Info.plist” file of the “SiriIntentExtension

<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>IntentsSupported</key>
<array>
<string>INSendMessageIntent</string>
</array>
</dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.intents-service</string>
<key>NSExtensionPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).IntentHandler</string>
</dict>

You need to denote the supported intents list. In case you are reducing the accessibility of the intents for the locked devices, it is important to detail out the restricted intents list with the help of IntentsRestrictedWhileLocked button.

On the other hand, we have the NSExtensionPrincipalClass key offering the name of the class in the IntentsExtension deciding the name of the handler for the intent. You can name this class as IntentHandler.

This is an easy step to follow:

import Intents
class IntentHandler: INExtension {
   override func handler(for intent: INIntent) -> Any? {
     return MessagesIntentHandler()
   }
}

As we can see there is a return of instance of MessagesIntentHandler for the specified intent as we focused only on a single intent type. But in case of various intents, you need to check the intent handler for

if intent is INSendMessageIntent {
  return MessagesIntentHandler()
}

The name MessagesIntentHandler is a class and the happening place. So if you want to incorporate this class you can follow the code below.

class MessagesIntentHandler: NSObject, 
INSendMessageIntentHandling {
...
}

You need the support of NSObjectProtocol if you want to run the INSendMessageIntentHandling protocol. This protocol has one compulsory and four other optional functions. If we talk about the four optional functions then these are:

The first is known as the resolveRecipients(forSendMessage:with:) where you have to specify the name of the recipient for the given intent. If you are using single intent, please acknowledge the entire suggested recipients. This will prevent Siri from asking the users to resolve the recipient issue.

func resolveRecipients(forSendMessage intent: INSendMessageIntent, with completion: @escaping ([INPersonResolutionResult]) -> Void) {
  guard let recipients = intent.recipients else {
    completion([])
    return
   }
   let resolutionResults = recipients.map { 
  INPersonResolutionResult.success(with: $0) }
    completion(resolutionResults)
}

Using the code above you have chosen the recipients from the intent to offer a solution for each of these.

The second optional function is resolveContent (forSendMessage: with 🙂. It helps in the validation of the input test. Check out the following code which is for filtering empty requests.

func resolveContent(forSendMessage intent: INSendMessageIntent, with completion: @escaping (INStringResolutionResult) -> Void) {
   if let text = intent.content, !text.isEmpty {
     completion(INStringResolutionResult.success(with: text))
   } else {
     completion(INStringResolutionResult.needsValue())  
   } 
 }

The users have to get authorized before they can use the app and get access to the SiriIntentExtention’s functionality. If you want to check the authorization of the users make sure the INSendMessageIntentHandling protocol has the confirm(sendMessage:completion:) function.

func confirm(sendMessage intent: INSendMessageIntent, completion: @escaping (INSendMessageIntentResponse) -> Void) {
   let userActivity = NSUserActivity(activityType: String(describing: INSendMessageIntent.self))
   let responseCode: INSendMessageIntentResponseCode
   if !NetworkReachability.isReachable {
     userActivity.userInfo = ["error": SiriIntentError.noInternetConnection]
     responseCode = .failureMessageServiceNotAvailable
   } else if !UserSession.hasAuthorizedSession() {
     userActivity.userInfo = ["error": SiriIntentError.notAuthorized]  
     responseCode = .failureRequiringAppLaunch
   } else {
    responseCode = .success
   }
   let response = INSendMessageIntentResponse(code: responseCode, userActivity: userActivity)
  completion(response)
}

You need to verify the authorization of the user along with checking the internet connectivity as well. Do remember that NSUserActivity is accessible only if the application is opened. If you are unable to verify you get an error such as failureRequiringAppLaunch.

Siri will prompt the user to open his application. Once the app gets opened, you would sort out the userActivity and probably retry for authorization. We can how get the code for the last main function such as

func handle(sendMessage intent: INSendMessageIntent, completion: @escaping (INSendMessageIntentResponse) -> Void) {
  MessageResolver.resolveSiriRequest(with: intent.content) { message in
   self.apiClient.send(message: message) { error in
    let responseCode: INSendMessageIntentResponseCode
    let userActivity = NSUserActivity(activityType: NSStringFromClass(INSendMessageIntent.self))
    if let error = error {
      userActivity.userInfo = ["error": error]
      responseCode = .failure
    } else {
      responseCode = .success
    }
    let response = INSendMessageIntentResponse(code: responseCode, userActivity: userActivity)
completion(response)
    }
  }
}

Here we try to solve the user domain issue by sending a wrapped message to the server intent.content. This is the user-recorded text message. In case you do not succeed in the task, you can send a failure response code. In such a case the Siri would inform the user that their request wasn’t successfully sent.

If you want to get more information on integration of the MessageIntentHandler, do consult the GitHub. Don’t forget to add the NSSiriUsageDescription key to the app’s Info.plist.

The Testing

Now the last task is testing whether the integration of Siri in third-party app was successful or not. In order to do so we have to imagine any app name for example, say Beautiful and the NSSiriUsageDescription.

After this try using Siri and say Beautiful a message saying test message. You get across to the next screen as below:

testing

Well, there is another alternative as well in which you can access the functionality with the process of authorization, which will lead you to the screen below:

testing

Topnotch Third-Party Apps using Siri

It is noteworthy that some top-notch brands are using Siri in their third-party apps. Some worthy names include Uber, Skype, Lyft, Pinterest, Whatsapp, LinkedIn, and others.

Important Tips for Developing the Extension

  • In case, you are using the network requests, make sure to compulsorily add the domains for both the app as well as the SiriIntentExtension to extensions for availing the App Transport Security Settings.
  • The Apple docs offer you the scope to integrate your own tailor-made dictionary of app-specific and user-specific words that Siri can actually misinterpret.
  • By using the command INPreferences.requestSiriAuthorization, you can manually ask for permission for using Siri.
  • Siri will only identify the app through its name. In case of two languages such as English and German app localizations, the Siri will not be able to help out user without specific localization name if the user chooses German as Siri language. But you can sort out this issue by offering the CFBundleDisplayName” key for localization with exact value.

The Concluding Lines

SiriKit is still a new feature that has made its inception in the recent times. Therefore, the iOS app developers may take some time to have a better understanding of the framework. One of the aspects that can create problems for user is limitations of words. However, Apple is trying to address this issue and upgrading the framework to make it more flexible.

We know that Apple has launched its latest iOS 12.1 version with several new features and functionaries. The company is planning to expand the range of app domains and add a few more into the list.

If you want to integrate Siri in the third-party apps, then you can rely on one of the proficient App Development Company and get in touch with the experts.

Bhumi Goklani

Bhumi Goklani

She is a project leader at Mindinventory having the past experience as an iOS developer who loves exploring everything that’s in trend! In her free time, she likes to delve into various project management tools and apply her findings in her projects.

Turn your vision to magnificent reality With
Our Web and Mobile Solutions