【发布时间】:2017-10-26 18:31:52
【问题描述】:
我正在尝试将 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") 案例 continueInApp
我知道.handleInApp 在后台打开应用程序,而continueInApp 打开应用程序。
但是当我使用 handleInApp 并向 Siri 说“使用我的 AppName 开始锻炼”时,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
}
应用程序委托方法被正确调用。
虽然我在这里发现了类似的问题
Siri always responds 'Continue in the app' when starting workout with SiriKit
但它使用已弃用的 continueInApp。所以不能在 iOS 11 中使用它。
我在这里犯了什么错误?为什么使用handleInApp 打不开应用,为什么Siri 抱怨我需要在应用中继续!如何使用handleInApp打开应用。
如果我不应该在使用锻炼意图时从 Siri 打开应用程序,我相信也无法为锻炼意图触发IntentUI。
参考:SiriKit, How to display response for start Workout Intent?
请帮忙。
【问题讨论】: