I am creating a standalone app for Apple Watch that measure heart beat rate every 30mn and send them to the server. I am referring to this projet to extract the heart beat rate. The project contain a class that extract this measure via HealthKit.
What I need to do is to make this task running permanently, even if the app in inactive. And be able to disable it via a settings view in my app.
So my question is: how to perform a task permanently in background in an Apple Watch application?
Thank you.
Look what they say in documentation:
Workout sessions let apps continue to run in the foreground.
So you app can gather heart rate information while app is inactive. Then you can use workoutSession(_:didChangeToState:fromState:date:)
method and split information into 30 minute intervals according to date property.
If your application is suspended, the delegate receives this call after the application resumes. This means you may receive the notification long after the state changed. Check the date parameter to determine when the state change actually occurred.
Finally you can send information to server when app becomes active.
You also can call openParentApplication:reply:
inside workoutSession(_:didChangeToState:fromState:date:)
to send stats to server in the background (on iphone).