SiriKit, How to display response for start Workout

2019-02-18 05:42发布

IntentHandler class:

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 documentation says:

enter image description here

Siri opens the app, but I need to display UI from IntentUI. How to do this?

In other words: How to prepare to display response, load intents UI extension, prepare interface and display it in code?

enter image description here

IntentViewController class:

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
    }
}

Base on this tutorial it is possible indeed.

enter image description here

1条回答
叛逆
2楼-- · 2019-02-18 05:57

Although Apple says here:

You can provide an Intents UI extension if you are supporting intents in the following domains:
Messaging
Payments
Ride booking
Workouts

I think it is quite impossible, because response responsible for open UI is not prepared for this:

Please look at INSendMessageIntentResponseCode for INSentMessageIntentHandling:

public enum INSendMessageIntentResponseCode : Int {


    case unspecified

    case ready

    case inProgress

    case success

    case failure

    case failureRequiringAppLaunch

    case failureMessageServiceNotAvailable
}

and INStartWorkoutIntentResponse for INStartWorkoutIntentHandling:

public enum INStartWorkoutIntentResponseCode : Int {


    case unspecified

    case ready

    case continueInApp

    case failure

    case failureRequiringAppLaunch

    case failureOngoingWorkout

    case failureNoMatchingWorkout
}

For the second one, there is only .continueInApp, and this is exactly what happens here, opposite to the first one where exists: .success and .inProgress

查看更多
登录 后发表回答