如何设置不是InterfaceController的HKWorkoutSessionDelegate

时间:2018-04-19 00:12:39

标签: ios swift delegates watchkit health-kit

大多数HealthKit示例代码都使用InterfaceController作为HKWorkoutSessionDelegate但是我试图设置一个HealthStoreManager类,它将作为HKWorkoutSessionDelegate并将此业务逻辑带走从我个人InterfaceControllers的任何责任,但以下代码无法开始锻炼,任何人都可以看到我的错误?

 InterfaceController: {

         func segueToWorkoutInterfaceControllerWithContext() {

                // Create workout configuration
                let workoutConfiguration = HKWorkoutConfiguration()

                workoutConfiguration.activityType = .running
                workoutConfiguration.locationType = .outdoor  

                do {
                    let workoutSession = try HKWorkoutSession(configuration: workoutConfiguration)
                    let healthStoreManager = HealthStoreManager()
                    workoutSession.delegate = healthStoreManager
                    healthStoreManager.start(workoutSession)
                } catch {
                    fatalError(error.localizedDescription)
                }


                let contextDictionary = ["workoutConfiguration" : workoutConfiguration, "ActivityType": selectedActivityType] as [String : Any]

                // Pass configuration to next interface controller
                WKInterfaceController.reloadRootPageControllers(withNames: ["WorkoutControlsInterfaceController", "MainDisplayInterfaceController", "SpeedInterfaceController", "CaloriesAndDistanceInterfaceController"],
                                                                contexts: [contextDictionary],
                                                                orientation: .horizontal,
                                                                pageIndex: 1)
            }

        }
    }


class HealthStoreManager: NSObject, CLLocationManagerDelegate, HKWorkoutSessionDelegate {
 private let healthStore = HKHealthStore()

// MARK: - HKWorkoutSessionDelegate

    func workoutSession(_ workoutSession: HKWorkoutSession, didFailWithError error: Error) {
        print("workout session did fail with error: \(error)")
    }

    func workoutSession(_ workoutSession: HKWorkoutSession,
                        didChangeTo toState: HKWorkoutSessionState,
                        from fromState: HKWorkoutSessionState,
                        date: Date) {
        DispatchQueue.main.async {
            self.handleWorkoutSessionState(didChangeTo: toState, from: fromState)
        }
    }

    func workoutSession(_ workoutSession: HKWorkoutSession, didGenerate event: HKWorkoutEvent) {
        DispatchQueue.main.async {
            //self.healthStoreManager.workoutEvents.append(event)
        }
    }

    private func handleWorkoutSessionState(didChangeTo toState: HKWorkoutSessionState,
                                           from fromState: HKWorkoutSessionState) {
        switch (fromState, toState) {
        case (.notStarted, .running):

            print("workout started")

        case (_, .ended):


        default:
            break
        }


    }


}

1 个答案:

答案 0 :(得分:1)

我发现了我的错误,我需要将healthStoreManager实例传递到上下文中,以避免此类被释放:

  let contextDictionary = ["workoutConfiguration" : workoutConfiguration, "ActivityType": selectedActivityType, "healthStoreManager" : healthStoreManager] as [String : Any]