使用`handleInApp` WorkoutIntentResponseCode Siri总是说"对不起,您需要继续使用App"不像已弃用的`continueInApp`

时间:2017-10-26 18:31:52

标签: ios sirikit

我正在尝试将Siri WorkOut意图集成到我的应用程序中。我感到很奇怪。

class StartWorkOutRequestHandling : NSObject, INStartWorkoutIntentHandling {
    func handle(startWorkout intent: INStartWorkoutIntent, completion: @escaping (INStartWorkoutIntentResponse) -> Void) {
        if let _ = intent.workoutName {
            if #available(iOSApplicationExtension 11.0, *) {
                let userActivity = NSUserActivity(activityType: "test")
                let response = INStartWorkoutIntentResponse(code: .handleInApp, userActivity: userActivity)
                completion(response);
            }
            else {
                // Fallback on earlier versions
                let userActivity = NSUserActivity(activityType: "test")
                let response = INStartWorkoutIntentResponse(code: .continueInApp, userActivity: userActivity)
                completion(response)
            }

        }
        else {
            let response = INStartWorkoutIntentResponse(code: .failureNoMatchingWorkout, userActivity: .none)
            completion(response);
        }
    }
}

正如您在handle(startWorkout中所看到的那样,检查iOS版本是否为11或更高,如果是,我使用handleInApp响应代码,否则我使用continueInApp。这是因为

public enum INStartWorkoutIntentResponseCode : Int {的定义说

  

@available(iOS,介绍:10.0,不推荐使用:11.0,消息:   " INStartWorkoutIntentResponseCodeContinueInApp在iOS上已弃用。   请改用INStartWorkoutIntentResponseCodeHandleInApp")       case continueInApp

我了解.handleInApp在后​​台打开应用,其中continueInApp打开应用。

但是,当我使用handleInApp并说"使用我的AppName开始锻炼" Siri说Siri说,

  

"抱歉,您需要继续使用应用程序"

并提供Open MyApp按钮。另一方面,如果我简单地使用

    func handle(startWorkout intent: INStartWorkoutIntent, completion: @escaping (INStartWorkoutIntentResponse) -> Void) {
    if let _ = intent.workoutName {
        let userActivity = NSUserActivity(activityType: "test")
        let response = INStartWorkoutIntentResponse(code: .continueInApp, userActivity: userActivity)
        completion(response);
    }
    else {
        let response = INStartWorkoutIntentResponse(code: .failureNoMatchingWorkout, userActivity: .none)
        completion(response);
    }
}

忽略警告Siri按预期打开应用程序。值得注意的是两种情况

- (BOOL)application:(UIApplication *)application
continueUserActivity:(NSUserActivity *)userActivity
 restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler {
    //this is a legacy app hence part of code is still in Objective-C
} 

app delegates方法被正确调用。

虽然我在这里发现了类似的问题

Siri always responds 'Continue in the app' when starting workout with SiriKit

但它使用了弃用的continueInApp。所以无法在iOS 11中使用它。

我在这里弄错了什么?为什么使用handleInApp无法打开应用程序,为什么Siri抱怨我需要继续使用App!如何使用handleInApp打开应用。

如果我不想在使用Workout意图时从Siri打开App,我相信也无法为Workout意图触发IntentUI。 请参阅:SiriKit, How to display response for start Workout Intent?

请帮忙。

0 个答案:

没有答案