我正在尝试在WatchOS上创建自定义SiriKit快捷方式。我读到此question是关于iOS的问题,但我认为在watchOS中解决方案有所不同。
为了做到这一点,我遵循了以下步骤:
class HandWashIntentHandler: NSObject, HandWashIntentHandling{
func handle(intent: HandWashIntent, completion: @escaping (HandWashIntentResponse) -> Void) {
let activity = NSUserActivity(activityType: "HandWashIntent")
let title = "Hand Wash"
activity.title = title
activity.suggestedInvocationPhrase = "Start hand wash"
activity.isEligibleForPrediction = true
activity.persistentIdentifier = title
let response = HandWashIntentResponse(code: .continueInApp,
userActivity: activity)
completion(response)
}
}
class IntentHandler: INExtension {
override func handler(for intent: INIntent) -> Any {
guard intent is HandWashIntent else {
fatalError("Unhandled intent type: \(intent)")
}
return HandWashIntentHandler()
}
}
所以,基本上,我希望通过语音快捷方式“开始洗手”。此快捷方式将打开应用程序并推送特定的控制器。我在question中看到,watchOS中的WKExtensionDelegate与iOS中的AppDelegate“等效”。然后,我尝试在ExtensionDelegate中操纵userActivity,但是没有函数可以这样做。
然后,我在ExtensionDelegate中使用了函数handle(_:):
func handle(_ userActivity: NSUserActivity) {
if userActivity.activityType == "HandWashIntent"{
WKExtension.shared().rootInterfaceController?.pushController(withName: "Wash", context: nil)
}
}
但没有成功。因此,如何检查我的应用是否由WatchOS上的Siri快捷方式启动?
在AppDelegate类上检查此方法。 launchOptions
字典,指示启动应用程序的原因。从LaunchOptionsKey
您将获得信息。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}