My workout app is experiencing very strange behaviour, observed with both an Watch SE on watchOS 9.0 and a Series 6 on Beta 9.1
At random times, without leaving the app, changing app settings or any other user input, the authorizationStatus of coreLocation reverts back to .notDetermined, something I thought wasn't even possible.
At launch, the app creates an object of my class WorkoutManager like so
RunApp.swift
import SwiftUI
@main
struct Runn_Watch_AppApp: App {
@StateObject private var workoutManager = WorkoutManager()
var body: some Scene {
WindowGroup {
WorkoutView()
}
.environmentObject(workoutManager)
}
}
The WorkoutManager class handles the healthStore and coreLocation like so
workoutManager.swift
class WorkoutManager: NSObject, ObservableObject, CLLocationManagerDelegate
{
let healthStore = HKHealthStore()
var workoutSession: HKWorkoutSession?
var workoutBuilder: HKLiveWorkoutBuilder?
let locationManager = CLLocationManager()
var routeBuilder: HKWorkoutRouteBuilder?
override init() {
super.init()
locationManager.delegate = self
requestCoreLocationAuthorization()
requestHealthStoreAuthorization()
}
func requestCoreLocationAuthorization()
{
if locationManager.authorizationStatus == .notDetermined {
locationManager.requestWhenInUseAuthorization()
}
locationManager.allowsBackgroundLocationUpdates = true
}
}
This is all almost boilerplate code, and it works great. I can start a run, the location data comes in fine and is written to the healthStore after completing a workout.
However, after completing a workout and syncing with my iPhone, authorizationStatus often (but not always) gets set back to .notDetermined.
When I stop and start new workouts on the watch, while my iPhone is not nearby, the authorizationStatus doesn't change. It only seems to happen when the iPhone is connected to the watch.
Any ideas what can cause the authorizationStatus to revert back to default?