使用WatchKit强制触摸MenuItem调用外部函数

tym*_*mac 5 swift healthkit watchkit

我需要实现一个WatchKit force-touch MenuItem来调用一个saveWorkout()位于不是子类的单独类中的方法WKInterfaceController.

我意识到每个类至少需要一个指定的初始化器.我猜这是关键?

顺便说一下,我的" saveSession()到达 "print语句在使用sim时登录到控制台,但在我使用设备时没有.即使使用设备,所有其他打印语句也会登录到控制台.有点奇怪.

我在初始化时的尝试会抛出各种错误,例如:

1.fatal错误:对类'DashboardController'使用未实现的初始化程序'init()'

2.在调用中删除参数"context"的参数

Dashboard.swift

class DashboardController: WKInterfaceController {

@IBOutlet var timerLabel: WKInterfaceTimer!
@IBOutlet weak var milesLabel: WKInterfaceLabel!

// var wSM: WorkoutSessionManager

//init(wSM: WorkoutSessionManager) {
//  self.wSM = wSM
//  super.init()
//  }


override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)

    addMenuItemWithItemIcon(.Accept, title: "Save", action: #selector(DashboardController.saveSession))
}

override func willActivate() {
    super.willActivate()
    print("Dashboard controller reached")
}

func saveSession() {
    //wSM.saveWorkout()
    print("saveSession() reached")    
    }
Run Code Online (Sandbox Code Playgroud)

WorkoutSessionManager.swift

class WorkoutSessionContext {

let healthStore: HKHealthStore
let activityType: HKWorkoutActivityType
let locationType: HKWorkoutSessionLocationType

init(healthStore: HKHealthStore, activityType: HKWorkoutActivityType = .Other, locationType: HKWorkoutSessionLocationType = .Unknown) {

    self.healthStore = healthStore
    self.activityType = activityType
    self.locationType = locationType
}
}

protocol WorkoutSessionManagerDelegate: class {
// ... protocol methods
}

class WorkoutSessionManager: NSObject, HKWorkoutSessionDelegate {

let healthStore: HKHealthStore
let workoutSession: HKWorkoutSession

init(context: WorkoutSessionContext) {
    self.healthStore = context.healthStore
    self.workoutSession = HKWorkoutSession(activityType: context.activityType, locationType: context.locationType)
    self.currentActiveEnergyQuantity = HKQuantity(unit: self.energyUnit, doubleValue: 0.0)
    self.currentDistanceQuantity = HKQuantity(unit: self.distanceUnit, doubleValue: 0.0)

    super.init()

    self.workoutSession.delegate = self
}

func saveWorkout() {
    guard let startDate = self.workoutStartDate, endDate = self.workoutEndDate else {return}

// ...code...
Run Code Online (Sandbox Code Playgroud)

jam*_*esk 4

致命错误是(或曾经)由这一行引起的:

let wSM = WorkoutSessionManager()

该行创建 WorkoutSessionManager 的新实例并调用init()它。

Swift 提供了一个默认初始值设定项,可init()调用任何为其所有属性提供默认值的结构或类,并且本身不提供至少一个初始值设定项。但是 WorkoutSessionManager 不提供healthStoreworkoutSession属性的默认值(并且这些属性不是可选的),并且它提供了自己的名为 的初始值设定项init(context:),因此它没有默认初始值设定项。

您需要使用指定的初始值设定项init(context:)(传递适当的 WorkoutSessionContext 实例)创建 WorkoutSessionManager 实例,或者为名为 的 WorkoutSessionManager 提供默认初始值设定项init()

执行前者的确切方式取决于应用程序其余部分的实现以及 DashboardController 的呈现。我假设您正在尝试重新创建WWDC 2015 Session 203中显示的“Fit”应用程序。

在该演示中,初始控制器是 ActivityInterfaceController 的一个实例,该控制器负责呈现下一个界面(通过故事板中创建的 segues)。可以在ActivityInterfaceController类中看到如下代码:

override func contextForSegueWithIdentifier(segueIdentifier: String) -> AnyObject? {
    let activityType: HKWorkoutActivityType

    switch segueIdentifier {
    case "Running":
        activityType = .Running

    case "Walking":
        activityType = .Walking

    case "Cycling":
        activityType = .Cycling

    default:
        activityType = .Other
    }

    return WorkoutSessionContext(healthStore: self.healthStore, activityType: activityType)
}
Run Code Online (Sandbox Code Playgroud)

上面的函数使用初始控制器持有的 HKHealthStore 实例创建并返回 WorkoutSessionContext 的新实例。该函数返回的上下文通过 传递到相关 segue 的目标接口控制器awakeWithContext

对于代码中的转换,您可以使用等效函数传递上下文实例,例如pushControllerWithName(context:)也导致awakeWithContext.

如果您的初始控制器与上面类似,您可以访问awakeWithContextDashboardController 类中传递的上下文,并使用它来配置 WorkoutSessionManager 的新实例:

class DashboardController: WKInterfaceController
{
    // ...

    var wSM: WorkoutSessionManager?

    override func awakeWithContext(context: AnyObject?) {
        super.awakeWithContext(context)

        if context is WorkoutSessionContext {
            wSM = WorkoutSessionManager(context: context as! WorkoutSessionContext)
        }

        addMenuItemWithItemIcon(.Accept, title: "Save", action: #selector(DashboardController.saveSession))
    }

    // ...
}
Run Code Online (Sandbox Code Playgroud)

以这种方式创建 WorkoutSessionManager 实例可以避免调用(不存在的)init()初始化程序,并允许重用 HKHealthStore 实例。该方法是否对您开放取决于您的代码的其余部分以及您呈现 DashboardController 的方式。

请注意,您应该避免创建 WorkoutSessionManager 的多个实例。使用单例提供在您的扩展中共享的 WorkoutSessionManager 的单个实例。