SiriKit,如何显示开始锻炼意图的响应?

时间:2016-12-08 18:36:24

标签: ios swift siri sirikit

IntentHandler 类:

import Intents

class IntentHandler: INExtension, INStartWorkoutIntentHandling {

    public func handle(startWorkout intent: INStartWorkoutIntent, completion: @escaping (INStartWorkoutIntentResponse) -> Void) {

        let userActivity = NSUserActivity(activityType: NSStringFromClass(INStartWorkoutIntent.self))
        let response = INStartWorkoutIntentResponse(code: .continueInApp, userActivity: userActivity)
        completion(response)
    }

    //MARK: - INStartWorkoutIntentHandling

    func confirm(startWorkout intent: INStartWorkoutIntent, completion: @escaping (INStartWorkoutIntentResponse) -> Void) {

        completion(INStartWorkoutIntentResponse(code: .continueInApp, userActivity: nil))
    }
}

Apple文档说:

enter image description here

Siri打开应用程序,但我需要显示IntentUI的UI。怎么做?

换句话说:如何准备显示响应,加载意图UI扩展,准备界面并在代码中显示它?

enter image description here

IntentViewController 类:

import IntentsUI

class IntentViewController: UIViewController, INUIHostedViewControlling {

    //MARK: - INUIHostedViewControlling

    func configure(with interaction: INInteraction!, context: INUIHostedViewContext, completion: ((CGSize) -> Void)!) {

        if let completion = completion {
            completion(self.desiredSize)
        }
    }

    var desiredSize: CGSize {
        return self.extensionContext!.hostedViewMaximumAllowedSize
    }
}

基于this教程,确实可以。

enter image description here

1 个答案:

答案 0 :(得分:1)

虽然Apple说here

  

如果您支持以下域中的意图,则可以提供Intents UI扩展:
  消息
  付款
  乘车预订
  锻炼

我认为这是不可能的,因为负责开放UI的响应没有为此做好准备:

INSendMessageIntentResponseCode查看INSentMessageIntentHandling

public enum INSendMessageIntentResponseCode : Int {


    case unspecified

    case ready

    case inProgress

    case success

    case failure

    case failureRequiringAppLaunch

    case failureMessageServiceNotAvailable
}

INStartWorkoutIntentResponse INStartWorkoutIntentHandling

public enum INStartWorkoutIntentResponseCode : Int {


    case unspecified

    case ready

    case continueInApp

    case failure

    case failureRequiringAppLaunch

    case failureOngoingWorkout

    case failureNoMatchingWorkout
}

对于第二个,只有.continueInApp,这正是此处发生的情况,与存在的第一个相反:.success.inProgress