Skip to content

Commit

Permalink
Merge pull request #94 from UbiqueInnovation/bugfix/allow-background-…
Browse files Browse the repository at this point in the history
…updates

Fix background location update logic
  • Loading branch information
ubfelix authored Jun 19, 2024
2 parents 8279022 + 5fb23c0 commit 6987310
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
6 changes: 3 additions & 3 deletions Sources/UBLocation/UBLocationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class UBLocationManager: NSObject {
private func delegates(onlyActive: Bool = false, usage: Set<LocationMonitoringUsage>? = nil) -> [UBLocationManagerDelegate] {
delegateWrappers.values.compactMap { wrapper in
if onlyActive {
wrapper.wantsUpdate(for: usage, isBackground: appIsInBackground) ? wrapper.delegate : nil
wrapper.wantsUpdate(for: usage, isBackground: appIsInBackground, allowsBackgroundLocationUpdates: allowsBackgroundLocationUpdates) ? wrapper.delegate : nil
} else {
wrapper.delegate
}
Expand Down Expand Up @@ -494,7 +494,7 @@ public class UBLocationManager: NSObject {
/// :nodoc:
private func startLocationMonitoringWithoutChecks(_ delegate: UBLocationManagerDelegate, usage: Set<LocationMonitoringUsage>) {
if usage.containsLocation {
if !appIsInBackground || usage.contains(.backgroundLocation) {
if !appIsInBackground || usage.contains(.backgroundLocation) || allowsBackgroundLocationUpdates {
locationManager.startUpdatingLocation()
startLocationTimer()
}
Expand All @@ -506,7 +506,7 @@ public class UBLocationManager: NSObject {
locationManager.startMonitoringVisits()
}
if usage.containsHeading {
if !appIsInBackground || usage.contains(.backgroundHeading) {
if !appIsInBackground || usage.contains(.backgroundHeading) || allowsBackgroundLocationUpdates {
locationManager.startUpdatingHeading()
}
}
Expand Down
26 changes: 15 additions & 11 deletions Sources/UBLocation/UBLocationManagerDelegateWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,25 @@ class UBLocationManagerDelegateWrapper {
self.usage = usage
}

func wantsUpdate(for usg: Set<UBLocationManager.LocationMonitoringUsage>?, isBackground: Bool) -> Bool {
guard let usg else {
return isBackground ? usage.requiresBackgroundUpdates : true
func wantsUpdate(for usg: Set<UBLocationManager.LocationMonitoringUsage>?, isBackground: Bool, allowsBackgroundLocationUpdates: Bool) -> Bool {
// First, determine which usage we are checking against:
// If a usage is given, we check whether we want an update for a specific usage set
// If no usage is given, we just answer whether we want updates at the moment, given our usage set and app state (background or foreground)
let usageToCheck = if let usg {
usage.intersection(usg)
} else {
usage
}

let intersection = usage.intersection(usg)

if intersection.isEmpty {
// If the usage set is empty, we are not interested in this update, regardless of app state
if usageToCheck.isEmpty {
return false
}

if !isBackground {
return true
}

return intersection.requiresBackgroundUpdates
// If the app is in the background, we only want an update if our usage
// requires background updates or if 'allowsBackgroundLocationUpdates' is set to true,
// in which case we can reveive updates even without background permissions.
// If we're in the foreground, we always want the update.
return isBackground ? usageToCheck.requiresBackgroundUpdates || allowsBackgroundLocationUpdates : true
}
}

0 comments on commit 6987310

Please sign in to comment.