IntentHandler for project that contains both Siri & Widget - ios14

I added Siri to my project and used IntentHandler to handle Siri's intents.
I then added Widget to my project so needed to handle its intents also.
I extended my IntentHandler's handler function to handle also Widget intents and it works, however I don't understand where in my xcode project the Widget is configured to use the same IntentHandler, and I even see that this file is not included in Widget target so don't understand how it works.
Didn't find anywhere in documentation an explanation on how to configure which intent handler will handle intents, and specifically if it's possible to define multiple IntentHandler instances, one to handle Siri's intent and another one to handle Widget's intents, and if yes how to do so.
I asked this question also in Apple Developer Forums but didn't get any response.

where in my xcode project the Widget is configured to use the same IntentHandler
In the Widget extension you only specify which Intent to use (by implementing IntentConfiguration, IntentTimelineProvider etc).
In the IntentHandler extension you specify which Intents to support.
It's all connected by the parent app and the type of Intent. IntentHandler doesn't know anything about Widget (and vice versa).
how to configure which intent handler will handle intents
It can be configured in the Info.plist file of the IntentHandler extension. Specifically in the IntentsSupported section:
if it's possible to define multiple IntentHandler instances, one to
handle Siri's intent and another one to handle Widget's intents
Yes, you can create two IntentHandler extensions and select which Intents to support in the IntentsSupported section (as in the image above).
Here is a GitHub repository with different Widget examples including the Dynamic Intent Widget.

Related

How to handle intents with parameters on SwiftUI?

I am trying to implement voice interaction on iOS with SwiftUI and Siri. I am just trying to get a shortcut to work with a single string parameter. I am very new to the framework and have been stuck. Below is what I tried:
Started on the Scrumdinger project (from an Apple SwiftUI tutorial, which I have in a GitHub repo. I stopped at commit 90731 before any Siri updates)
Created a button to add a Siri shortcut based on NSUserActivity to create a new scrum, but without any parameters; this worked (d64133)
Afterwards, I tried a bunch of things that didn't work:
Created an intent extension target with a group to share
Created an intent with a parameter, naming it "ScrumIntent"
Created a shortcut for the intent
Requested authorization for Siri
Currently, my set up has all the above and I can create the shortcut, which appears on the shortcuts app. However, it gets stuck and I don't even know how to debug it. If I try voice, Siri complains something went wrong. If I try to click on the shortcut, it is also stuck. Below you can see an image of how it gets stuck, with a stop button almost mid complete.
I am trying to simply set a string in user defaults when the callback gets handled. No special checking on the handler.
Some points about the implementation:
The handler is only in the extension target, but I am not sure if that's correct. In SwiftUI we can use the modifier onContinueUserActivity for NSUserActivities, but I couldn't find a similar one for intents (I understood we need intents for parameters).
The "Add Siri shortcut" button simply defines the Intent and sets suggestedInvocationPhrase.
Is there any other detail or essential piece of code I should share here? I was not sure so I just shared the github links for now.

How to integrate Siri with flutter application?

I want to integrate Siri in my flutter app for iOS. When the user asks a particular question,
Siri must get the output from the app and execute a function in the app. How can I achieve this?
any help will be appreciated.
In order to achieve this you will need the following:
Create your intent definition file in XCode
Add the Intents extension target to your Xcode project
Configure the Flutter extension home widget
https://pub.dev/packages/home_widget
Add app groups capabilities to both your Runner target and the Intents target make sure to use the same group id
Initialize the App on the flutter side with the same group id
Save the data that your Siri intent would need using the home widget extension methods
Use the saved data reading from UserDefaults
There are some things to consider here:
Siri's intents are in a different Target because they can run in the background if needed, this is why you cannot have a direct communication line between the intent and flutter (Yet).
Instead what you will need to do is to save in memory some data your user might need when using Siri maybe a user id or API key, if you need to make some HTTP calls you will have to do it from Swift.
This is why we need the home widget extension since Home widgets also use a different target and This extension opens a communication channel using App groups and UserDefaults we could use the same communication channel to send data to our Siri Intent.
Just follow the configuration guide of the HomeWidget extension and add AppGroups capabilities to your runner Target and also to your Siri intent. make sure you use the same group ID in all of them including on the flutter side:
HomeWidget.setAppGroupId('YOUR_GROUP_ID');
Then you can use the extension methods to send and receive data between flutter and swift.

Accessing App code from Notification Service Extension

Notification Service Extension modifies the content of a remote notification before it's delivered to the user. For example if a remote notification contains an image URL, Notification Service Extension can be used to fetch the image and show it in the notification content.
But how can I access actual app code in Notification Service Extension? Let's say I have a DataAccess swift class in app code, can I access this class in Notification Service Extension?
Edit: Some folks suggested to add app code to service extension, which is not applicable in my situation.
Go to the classes that you want to use in your app project. On your File inspector your are gonna see the Target Membership of your class. Check the extension's name checkbox. Then you can reuse your code.
You can also add the classes you need in the Build Phases -> Compiled Sources in your notification service target. The result is the same.
The usual solution is to create a library/framework which includes your DataAccess class, and your app and the Notification Service Extension uses this library/framework.
Make sure you select Do not embed for your framework when adding to Notification Service Extension, and select Embed and sign when adding it to your app.

How to send message from widget to iOS app

I'm implementing a widget extension with 3 buttons. These buttons are meant to trigger a method in my iOS app. Is this possible? I know you can share data with UserDefaults but I want to trigger a function right from the widget.
The best way to do it (and the way Apple suggests) is to create an embedded framework with all the common code and methods you need your main app and your widget to share and import that framework to both targets.

How to push different IntentViewController in Sirikit extension based on the Intent type?

I am trying to integrate Sirikit extension into my existing iOS app.
For this, I am going to use two different types of intent.
INSendMessageIntent - to send message from Siri through my app.
INSendPaymentIntent - for making a payment
For both the intent types, I was to use custom & different IntentUI.
But how to push different ViewController in Sirikit extension for based on the Intent type i.e., if I say Make payment using MyWorld then one PayViewController has to be pushed and when I say Send message using MyWorld then MessageViewController has to be pushed.
I want to choose View controller dynamically at runtime based on the Intent type (i.e., INSendMessageIntent or INSendPaymentIntent).
I think you should be able to support more than one function of Siri by creating independent targets. The payments UI is one IntentsUI target, and the messages UI is another one. Info plists for each define which target looks after which type of intent.

Resources