Я пытаюсь запустить функцию в фоновом режиме на iOS и watchOS, и я нашел пример кода, но у меня это не сработало.
Я попробовал некоторый пример кода, который я нашел на GitHub, и функцию потока рассылки.
..........
private func startWorkout() {
let workoutConfiguration = HKWorkoutConfiguration()
workoutConfiguration.activityType = .other
do {
workoutSession = try HKWorkoutSession(healthStore: healthStore, configuration: workoutConfiguration)
workoutSession?.delegate = self
// HKWorkoutSession.startActivity(workoutSession!)
healthStore.start(workoutSession!)
} catch {
print(error)
}
}
@objc fileprivate func vibrate() {
WKInterfaceDevice.current().play(.success)
}
........
extension InterfaceController: HKWorkoutSessionDelegate {
func workoutSession(_ workoutSession: HKWorkoutSession, didFailWithError error: Error) {
}
func workoutSession(_ workoutSession: HKWorkoutSession, didGenerate event: HKWorkoutEvent) {
}
func workoutSession(_ workoutSession: HKWorkoutSession, didChangeTo toState: HKWorkoutSessionState, from fromState: HKWorkoutSessionState, date: Date) {
switch toState {
case .running:
hapticFeedbackTimer = Timer(timeInterval: 0, target: self, selector: #selector(vibrate), userInfo: nil, repeats: true)
RunLoop.main.add(hapticFeedbackTimer!, forMode: .default)
default:
hapticFeedbackTimer?.invalidate()
hapticFeedbackTimer = nil
}
}
}```
I expected the function vibrate to be run in the background, but instead, nothing happened