具有自定义意图的Siri Integration中面临的问题

时间:2019-08-15 17:00:48

标签: ios swift siri sirikit sirishortcuts

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

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

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

enter image description here

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

enter image description here

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

enter image description here

enter image description here

  1. 验证并添加了用于自定义意图的新处理程序,并定义了resolve,处理和确认方法,如下所示。目前,我将随机返回no作为奖励积分。
//
//  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以返回用于奖励意图的奖励处理程序
//
//  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索取奖励积分。立即要求我定义的应用程序快捷方式。一旦我们说“是”要求,就需要询问我的密码。但是Siri回答我的应用存在一些问题。要求下一个参数值该怎么做。
  3. 在处理程序文件中,我为每个参数添加了resolve方法。我认为,不会调用resolve方法来验证值。我们需要处理任何事情以使解析方法起作用吗?
  4. 如何在resolve / handle / confirm方法中使用断点调试处理程序实现。

先谢谢了。

1 个答案:

答案 0 :(得分:1)

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

  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索取奖励积分。立即要求我定义的应用程序快捷方式。一旦我们说“是”要求,就需要询问我的密码。但是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.

谢谢。