Tech Hub

SiriKit

Extend Your iOS App With SiriKit

iOS 10 presented SiriKit, another system empowering applications to coordinate with Siri. Generally, your applications can send messages, place calls, send installments, and that's only the tip of the iceberg. SiriKit encourages you make new encounters on iOS and investigate plan standards for making an extraordinary Siri association. 

Siri is a basic piece of iOS. It has Intents.framework and contacts joining called CallKit. SiriKit support is partitioned into spaces, each of those characterizes at least one assignment that can be performed. So as to utilize SiriKit, applications must help one of the accompanying spaces: VoIP calling, Messaging, Payments, Photos, Workouts, Ride booking, Car orders, CarPlay (car merchants just), and Restaurant reservations (requires extra help from Apple). 

Intents and Intents UI App Extensions

SiriKit has two unique kinds of application extensions: 

1.    Required augmentation:   Goals augmentation gets purpose objects from the framework and plays out the related assignments. Expectations expansion is the premise of SiriKit and supports at least one plans, runs out of sight while Siri is dynamic. Additionally, executes the determination, affirm, and handle strategies. 

class IntentHandler: INExtension, INSendMessageIntentHandling, INSearchForMessagesIntentHandling, INSetMessageAttributeIntentHandling {
override func handler(for intent: INIntent) -> Any {
// This is the default implementation. If you want different objects to handle different intents,
// you can override this and return the handler you want for that particular intent.
return self
}

2.    Discretionary augmentation:   Purposes UI augmentation lets you tweak the presence of the Siri or Maps interface after a plan has been dealt with effectively. You can utilize an Intents UI expansion in the event that you are supporting plans in the accompanying spaces: 
•    messaging 
•    payments 
•    ride booking 
•    workouts 

Aims UI expansion brings your application's interface into Siri and gives a UIViewController Optional. 

class IntentViewController: UIViewController, INUIHostedViewControlling
// Prepare your view controller for the interaction to handle.
func configure(with interaction: INInteraction!, context: INUIHostedViewContext, completion: ((CGSize) -> Void)!) {
// Do configuration here, including preparing views and calculating a desired size for presentation.
if let completion = completion {
completion(self.desiredSize)
}
}

Including SiriKit 

To add SiriKit to your application: 

Empower Siri capacity in your application. 
1. In venture settings, select application target, and afterward select the abilities tab. 
2. Include Intents augmentation. In Xcode explore to Select File > New > Target. 
3. Set up the name of the augmentation, arrange the language and different choices. 
4. In the Info.plist document, explore to NSExtension > NSExtensionAttributes > IntentsSupported and indicate which Intents must be upheld. 

The IntentsRestrictedWhileLocked key (discretionary key) is the class name of the goal which you require the gadget to be opened for.

You can include more than one Intents augmentations to your application, however every expansion must help various aims. Clients must give authorization for your application to utilize SiriKit. Additionally, you should include the NSSiriUsageDescription key in your iOS application's Info.plist record.

How SiriKit Intents Work

There are 3 degrees of correspondence between your application and Siri about Intents: 

1.    Resolve:   Ask your application to determine each parameter on in plan. We will call settle not many occasions for each parameter and affirm client demand. This progression helps Siri comprehend what client stated, and all the more critically, what they implied when they sent the solicitation to your application. 

// MARK: - INSendMessageIntentHandling
// Implement resolution methods to provide additional information about your intent (optional).
func resolveRecipients(forSendMessage intent: INSendMessageIntent, with completion: @escaping ([INPersonResolutionResult]) -> Void)
if let recipients = intent.recipients {
// If no recipients were provided we'll need to prompt for a value.
if recipients.count == 0 {
completion([INPersonResolutionResult.needsValue()])
return
}
var resolutionResults = [INPersonResolutionResult]()
for recipient in recipients {
let matchingContacts = [recipient] // Implement your contact matching logic here to create an array of matching contacts
switch matchingContacts.count {
case 2 ... Int.max:
// We need Siri's help to ask user to pick one from the matches.
resolutionResults += [INPersonResolutionResult.disambiguation(with: matchingContacts)]
case 1:
// We have exactly one matching contact
resolutionResults += [INPersonResolutionResult.success(with: recipient)]
case 0:
// We have no contacts matching the description provided
resolutionResults += [INPersonResolutionResult.unsupported()]
default:
break
}
}
completion(resolutionResults)
}
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())
}
}

2. Confirm:  Subtleties Siri about anticipated consequence of taking care of an aim. It is an open door for your application to check for any necessity states to finish the client demand. (For instance, if a client is confirmed) 

// Once resolution is completed, perform validation on the intent and provide confirmation (optional).
func confirm(sendMessage intent: INSendMessageIntent, completion: @escaping (INSendMessageIntentResponse) -> Void) {
// Verify user is authenticated and your app is ready to send a message.
let userActivity = NSUserActivity(activityType: NSStringFromClass(INSendMessageIntent.self))
let response = INSendMessageIntentResponse(code: .ready, userActivity: userActivity)
completion(response)
}

3. Handle:   Utilized for explicit activities a client demands. You ought to give however much data about the outcome as could reasonably be expected. System calls can require some serious energy and Siri shows the holding up liveliness. It can give a reaction inside a couple of moments, else, you can utilize the InProgress reaction code. 

// Handle the completed intent (required).
func handle(sendMessage intent: INSendMessageIntent, completion: @escaping (INSendMessageIntentResponse) -> Void) {
// Implement your application logic to send a message here.
let userActivity = NSUserActivity(activityType: NSStringFromClass(INSendMessageIntent.self))
let response = INSendMessageIntentResponse(code: .success, userActivity: userActivity)
completion(response)
}

Application explicit Vocabulary 

A great deal of applications have one of a kind methods for depicting things and Siri needs your help to get words and expressions remarkable to your application. A few expressions are portions of your application. You can make an application explicit jargon to portray words and expressions that are a piece of your application. 

These application explicit words and expressions are characterized in the plist record and can be limited. The jargon has 2 keys:

•    ParameterNames: The variety of String. (Properties to which the predefined jargon terms are applied). 

•    ParameterVocabulary: The variety of Dictionary. (The jargon terms, including any sort of elocution and models).

Share Linkedin