6

我想使用 SiriKit 开始锻炼。开始锻炼需要从应用程序扩展打开主应用程序。

Apple 为INStartWorkoutIntentHandling处理程序提供的样板文件是

func handle(startWorkout startWorkoutIntent: INStartWorkoutIntent, completion: (INStartWorkoutIntentResponse) -> Void) {
    let userActivity = NSUserActivity(activityType: NSStringFromClass(INStartWorkoutIntent))
    let response = INStartWorkoutIntentResponse(code: .success, userActivity: userActivity)
    completion(response)
}

如何从这里打开我自己的应用程序?myapp://workout?action=start&name=pushups This answer之类的东西似乎无关紧要,因为我没有UIViewController带有extensionContext此扩展类型的属性。

最后相关位:对于其他操作(暂停、结束),我宁愿不打开主应用程序,而是简单地暂停在主应用程序中运行的锻炼。我可以使用类似的自定义 URL 来暂停它,但这会打开应用程序,这是一个额外的不必要的步骤。有什么好方法可以告诉主应用程序在不打开应用程序的情况下从 INExtension 执行特定操作?

4

2 回答 2

2

对我来说,我能够像这样启动应用程序:

let activity  = NSUserActivity(activityType: NSUserActivityTypeBrowsingWeb)
activity.webpageURL = URL(string: "https://mywebsite/...")        
completion(MyIntentResponse(code: .continueInApp, userActivity: activity))

然后这将在 AppDelegate 中调用

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
        if let url = userActivity.webpageURL {
            handleDeeplink(url)
        }
    }
    return false
}
于 2019-07-11T20:30:11.417 回答
1

为了后代:原来这是 Xcode 8 beta 之间此功能的一个阶段,它已在 Xcode 8 beta 3 中得到解决。他们将.continueInApp代码添加到INStartWorkoutIntentResponseCode这个版本中,但它在 Xcode 8 beta 2 中不存在。这个状态码允许直接传递 NSUserActivity(无需使用 URL 方案)。

于 2016-07-23T16:18:17.393 回答