I need some help in tracking user location in background, suspended and killed state.
Problem Statement
I want to track user location after his consent in my App. I want to track user location in all the states Background, Suspended and Foreground. Till user has stopped it from the app. I am not able to track location in background and killed state but foreground is working fine.
I have also enabled below settings in my locationManager
locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = false
locationManager.showsBackgroundLocationIndicator = true
locationManager.startMonitoringSignificantLocationChanges()
What I have tried till now is
- Enabled background modes
- Location updates (checked)
- Background fetch (checked)
- Background processing (checked)
- Permission to Allow Always location access
Created a singleton for location tracking check the below code and calling this class on didFinishLaunchingWithOptions - AppDelegate
class EmployeeAttendanceTracker: NSObject,CLLocationManagerDelegate {
static let shared = EmployeeAttendanceTracker()
private let locationManager = CLLocationManager()
private var lastLocationDate = Date()
static let LOCATION_INTERVAL = 1
var locationUpdate: (() -> Void)?
private override init() {
super.init()
setupLocationManager()
}
private func setupLocationManager() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.activityType = .other
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = false
locationManager.showsBackgroundLocationIndicator = true
if #available(iOS 9.0, *) {
locationManager.requestAlwaysAuthorization()
} else {
locationManager.requestWhenInUseAuthorization()
}
}
//
//
// // MARK: - CLLocationManagerDelegate
//
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .restricted:
//showLocationPermissionAlert()
Logger.s("Location access restricted.")
case .denied:
//showLocationPermissionAlert()
Logger.s("User denied location access.")
case .notDetermined:
// showLocationPermissionAlert()
Logger.s("Location access not determined.")
case .authorizedAlways:
if #available(iOS 9, *) {
locationManager.requestLocation()
} else {
locationManager.startUpdatingLocation()
locationManager.startMonitoringSignificantLocationChanges()
}
default:
// showLocationPermissionAlert()
break
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
Logger.s("User latitude: \(location.coordinate.latitude), longitude: \(location.coordinate.longitude)")
locationManager.stopUpdatingLocation()
let now = Date()
if isItTime(now: now as NSDate) {
if shouldSendLocationToServer() {
self.sendLocationToServer(location: location,completion: {
self.locationUpdate?()
})
}else{
self.locationUpdate?()
}
Logger.s(now)
Logger.s(location)
}else{
self.locationUpdate?()
}
}
Any help on this would be greatly appreciated