The app in this sample project demonstrates how to send payments between users by implementing INSendPaymentIntentHandling. Users can make payments with Siri and view a list of previously completed payments inside the main app.
The project consists of three targets:
Payments, an iOS app which shows a history of completed payments.
PaymentsIntentsExtension, an Intent Extension that integrates with SiriKit to send payments.
PaymentsFramework, an embedded framework containing shared code needed by both the Intent Extension and the main app.
See Creating an Intents App Extension for more information on the general process of adding an Intent Extension to your app, including how to enable the Siri capability and configure the NSExtension keys in the Info.plist.
Configure the Sample Code Project
This sample app can be run in the iOS Simulator without any special setup, but in order to run on a device you will need to update the build settings and enable an App Group for the project:
Open Payments.xcodeproj with the latest version of Xcode.
In the project editor, set a new bundle identifier under Identity on the General pane for each of the three targets in the project.
In the Capabilities pane, make sure that App Groups is switched on for the Payments and PaymentsIntentsExtension targets.
Add an App Group identifier with the format group.com.example.
Reference the new App Group identifier in the source code. Open PaymentProvider.swift and modify the implementation of the sharedUserDefaults property to reference the new identifier.
Resolve Payment Parameters
The first step in processing a payment with INSendPaymentIntentHandling is to obtain the payee for the transaction and the currency amount to send to the payee. Depending on the command that the user speaks to Siri, your app may have to deal with one of the following scenarios:
The initial request specifies all required parameters. For example, the user requests, “Send $50 to Jane with Payments.”
The request lacks some required parameters. For example, the user requests, “Send money to Jane with Payments.”
The request supplies incorrect or inaccurate parameters. For example, the user requests, “Send money to Dave with Payments,” when no valid contacts match “Dave”.
The initial request supplies no parameters. For example, the user requests, “Send money with Payments.”
The extension resolves each parameter separately by responding to methods from the INSendPaymentIntentHandling protocol. Depending on the initial request and subsequent conversation with Siri, each of the parameter resolution methods may be called multiple times as Siri clarifies the request from the user.
Use INVocabulary to aid Siri with recognizing any domain-specific vocabulary users are likely to use in their voice commands. For example, in this sample project payee names are not taken from the user’s contacts but from a predefined list inside the app, so these names are added as additional vocabulary with setVocabularyStrings(_:of:).
Finally, once the payment has been confirmed by the user, the system calls handle(intent:completion:). Use this method to perform the confirmed transaction and pass back the status of the transaction by calling the completion block.