Siri 与自定义意图的集成面临问题

Facing issue in Siri Integration with custom intents

我正在尝试将 Siri 快捷方式集成到我的应用程序中。我正在尝试的概念是通过密码确认获得我的卡的奖励积分。请在下面找到我为此所做的工作。

  1. 在功能中启用了 Siri 并添加了 Siri Intent 定义文件。
  2. 添加了名为 say Rewards 的新自定义意图。

  3. 定义标题。启用确认的字幕和参数(accType,pin)。 Pin 将单独发送给用户。

  1. 然后使用参数“rewardPoints”定义意图响应并定义响应消息。

  1. 添加了 Siri 意图扩展。
  2. 向项目和意图扩展中的 info.plist 文件添加了自定义意图。

  1. 已验证并为自定义意图添加了新的处理程序,并定义了解析、处理和确认方法,如下所示。现在,我 return 随机拒绝奖励积分。
//
//  RewardsIntentHandler.swift
//  SiriIntentExt
//

import UIKit
import Intents

class RewardsIntentHandler: NSObject, RewardsIntentHandling {

    func resolveAccType(for intent:RewardsIntent, with completion: @escaping ([INStringResolutionResult]) -> Void) {
        guard let accType = intent.accType else {
            completion([INStringResolutionResult.needsValue()])
            return
        }

        completion([INStringResolutionResult.success(with: accType)])
    }

    func resolvePin(for intent:RewardsIntent, with completion: @escaping ([INIntegerResolutionResult]) -> Void) {

        guard let verifyPin = intent.pin else {
            completion([INIntegerResolutionResult.needsValue()])
            return
        }

        completion([INIntegerResolutionResult.confirmationRequired(with: verifyPin as? Int)])
    }

    func confirm(intent: RewardsIntent, completion: @escaping (RewardsIntentResponse) -> Void) {
        completion(RewardsIntentResponse.init(code: RewardsIntentResponseCode.ready, userActivity: nil))
    }

    func handle(intent: RewardsIntent, completion: @escaping (RewardsIntentResponse) -> Void) {

        guard intent.accType != nil else {
            completion(RewardsIntentResponse.init(code: RewardsIntentResponseCode.continueInApp, userActivity: nil))
            return
        }

        guard intent.pin != nil else {
            completion(RewardsIntentResponse.init(code: RewardsIntentResponseCode.continueInApp, userActivity: nil))
            return
        }

        let response = RewardsIntentResponse.success(rewardPoints: NSNumber(value: 3453))
        completion(response)
    }
}
  1. 将 IntentHandler 修改为 return 奖励意图的奖励处理程序
//
//  IntentHandler.swift
//  SiriIntentExt
//

import Intents

class IntentHandler: INExtension {

    override func handler(for intent: INIntent) -> Any {

        if intent is RewardsIntent {
            return RewardsIntentHandler()
        }

        return self
    }

}
  1. 捐赠意图如下。
//
//  ViewController.swift
//  Shortcuts
//

import UIKit
import Intents

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        siriAuthorisarion()
        donateRewardIntent()
    }

    func siriAuthorisarion()  {

        INPreferences.requestSiriAuthorization { (status) in
            print("Siri Authorization Status - ", status)
        }
    }

    func donateRewardIntent() {

        let rewardsIntent = RewardsIntent()
        rewardsIntent.suggestedInvocationPhrase = "Reward Points"
        rewardsIntent.accType = "test account"

        let interaction = INInteraction(intent: rewardsIntent, response: nil)

        interaction.donate { error in
            if let error = error {
                print("Donating intent failed with error \(error)")
            }

            DispatchQueue.main.async {
                let alert = UIAlertController.init(title: ((error != nil) ? "Error" : "Success"), message: ((error != nil) ? "Oops!!! Error occured on donating intent." : "Intent donated succussfully!!!"), preferredStyle: .alert)
                alert.addAction(UIAlertAction.init(title: "OK", style: .default, handler: nil))
                self.present(alert, animated: true, completion: nil)
            }
        }
    }
}

我遇到了上述代码库的问题。 Siri 没有请求密码,也无法获得该帐户的确切奖励积分。

有以下问题。

  1. 我们能否以编程方式将意图添加到 Siri,而不是从快捷方式应用程序或设置中添加。这样用户就可以在安装应用程序后直接使用该功能。
  2. 使用快捷方式应用程序添加意图后,我正在尝试向 Siri 询问奖励积分。它立即请求定义我的应用程序快捷方式。一旦我们说 'yes' 请求,我需要被要求提供 pin。但是 Siri 回复说我的应用程序有问题。要求下一个参数值要做什么。
  3. 在处理程序文件中,我为每个参数添加了解析方法。我觉得,没有调用 resolve 方法来验证值。我们是否需要处理任何事情才能使 resolve 方法起作用?
  4. 如何在 resolve/handle/confirm 方法中使用断点调试处理程序实现。

提前致谢。

以上问题找我的分析。

  1. 我们能否以编程方式将意图添加到 Siri,而不是从快捷方式应用程序或设置中添加。这样用户就可以在安装应用程序后直接使用该功能。

By default, intents are provided for specific domains such as messaging, payments, photos, workout, etc. No need to explicitly add intents through shortcuts for theses specific domains. Apart from these domains if we are creating custom intent, we are in need to donate and add the intents to Siri using shortcut/settings application.

  1. 使用快捷方式应用程序添加意图后,我正在尝试向 Siri 询问奖励积分。它立即请求定义我的应用程序快捷方式。一旦我们说 'yes' 请求,我需要被要求提供 pin。但是 Siri 回复说我的应用程序有问题。要求下一个参数值要做什么。

From iOS13, Apple has added Siri parameters and Siri suggestion for custom intents to request the missing parameters. Till iOS12, we don't have parameters option for custom intents.

  1. 在处理程序文件中,我为每个参数添加了解析方法。我觉得,没有调用 resolve 方法来验证值。我们是否需要处理任何事情才能使 resolve 方法起作用?

In iOS12, we cannot add resolve methods for parameters in custom intents. Resolve methods handled only for specific domains provided within Intents extensions as mentioned in question 1. From iOS13, we can have resolve methods for custom intents based on the parameters.

  1. 如何在 resolve/handle/confirm 方法中使用断点调试处理程序实现。

We can add breakpoints and debug intent handler methods.

谢谢。