Hello,
This is my first post in the forums, and I'm still learning my way with iOS Development and Swift. My apologies if the formatting is not correct, or If I'm making any mistakes.
I'm currently trying to implement an iOS App where the device needs to share the location with my server via an API call.
The use case is as follows: the server expects location updates to determine if a device is inside/outside a geofence. If the device is stationary, no locations need to be sent. If the device begins moving, regardless of whether the app is in foreground, background, or terminated, the app should resume posting locations to the server.
I've decided to use the CLLocationUpdate.liveUpdates() stream, together with CLBackgroundActivitySession().
However, I have not been able to achieve the behavior successfully. My app either maintains the blue CLActivitySession indicator active, regardless of whether the phone is stationary or not, or kills the Indicator (and the background capability) and does not restore it when moving again. Below I've attached my latest code snippet (the indicator disappears and does not come back).
// This method is called in the didFinishLaunchingWithOptions
func startLocationUpdates(precise: Bool) {
// Show the location permission pop up
requestAuthorization()
// Stop any previous sessions
stopLocationUpdates()
Task {
do {
// If we have the right authorization, we will launch the updates in the background
// using CLBackgroundActivitySession
if self.manager.authorizationStatus == .authorizedAlways {
self.backgroundActivity = true
} else {
self.backgroundActivity = false
self.backgroundSession?.invalidate()
}
// We will start collecting live location updates
for try await update in CLLocationUpdate.liveUpdates() {
// Handle deprecation
let stationary = if #available(iOS 18.0, *) {
update.stationary
} else {
update.isStationary
}
// If the update is identified as stationary, we will skip this update
// and turn off background location updates
if stationary {
self.backgroundSession?.invalidate()
continue
}
// if background activity is enabled, we restore the Background Activity Session
if backgroundActivity == true { self.backgroundSession = CLBackgroundActivitySession() }
guard let location = update.location else { continue }
// Do POST with location to server
}
} catch {
print("Could not start location updates")
}
}
}
I'm not sure why the code does not work as expected, and I believe I may be misunderstanding how the libraries Work. My understanding is that the liveUpdates stream is capable of emitting values, even if the app has gone to the background/terminated, thus why I'm trying to stop/resume the Background Activity using the "stationary" or "isStationary" attribute coming from the update.
Is the behavior I'm trying to achieve possible? If so, I'm I using the right libraries for it? Is my implementation correct? And If not, what would be the recommended approach?
Regards