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

RAJ*_*AJA 5 ios siri swift sirikit sirishortcuts

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

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

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

在此处输入图片说明

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

在此处输入图片说明

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

在此处输入图片说明

在此处输入图片说明

  1. 验证并为自定义意图添加了新的处理程序,并定义如下的解析、处理和确认方法。现在,我将随机返回奖励积分。
//
//  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)
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. 修改 IntentHandler 为奖励意图返回奖励处理程序
//
//  IntentHandler.swift
//  SiriIntentExt
//

import Intents

class IntentHandler: INExtension {

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

        if intent is RewardsIntent {
            return RewardsIntentHandler()
        }

        return self
    }

}
Run Code Online (Sandbox Code Playgroud)
  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)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我正面临来自上述代码库的问题。Siri 不要求输入密码,也无法获得该帐户的确切奖励积分。

有以下问题。

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

提前致谢。

RAJ*_*AJA 2

找到我对以上问题的分析。

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

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.

\n\n
    \n
  1. 使用“快捷方式”应用添加意图后,我\xe2\x80\x99m 尝试向 Siri 询问奖励积分。它立即请求定义我的应用程序快捷方式。一旦我们同意请求,我就需要被要求提供密码。但 Siri 回复我的应用程序存在一些问题。询问下一个参数值时要做什么。
  2. \n
\n\n

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.

\n\n
    \n
  1. 在处理程序文件中,我为每个参数添加了解析方法。我觉得,解析方法没有被调用来验证值。我们需要处理什么才能使解析方法起作用吗?
  2. \n
\n\n

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.

\n\n
    \n
  1. 如何在解析/处理/确认方法中使用断点来调试处理程序实现。
  2. \n
\n\n

We can add breakpoints and debug intent handler methods.

\n\n

谢谢。

\n