1

I want to send user current Location to a server in every 15 minutes. When the app is in the foreground the timer is running but when the app is in background timer is not running. I searched a lot but did not find any solution. Can you please tell me what I do. I am using below code for background location sync but it's not working.

    var locationManager = CLLocationManager()
    var currentLocation: CLLocation?
    var timer : Timer?
    var backgroundTaskIdentifier: UIBackgroundTaskIdentifier?

   func getCurrentLocation()
    {
        // Do any additional setup after loading the view, typically from a nib.
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.allowsBackgroundLocationUpdates = true
        locationManager.pausesLocationUpdatesAutomatically = false
        //
        backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(expirationHandler: {
            UIApplication.shared.endBackgroundTask(self.backgroundTaskIdentifier!)
        })
        let timer = Timer.scheduledTimer(withTimeInterval: 10.0, repeats: true) { (timer) in
            self.locationManager.startUpdatingLocation()
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        print("locations = \(locations)")
        if  let location = locations.last
        {
            print("location\(String(describing: location.coordinate))")
            currentLocation = location
            locationManager.stopUpdatingLocation()
        }
    }
Dilip Tiwari
  • 1,441
  • 18
  • 31
gaurav gupta
  • 115
  • 2
  • 11

2 Answers2

0

I am also having similar kind of requirement but time interval is 5minutes.

May be your app in idle for longer time(15 minutes) that's y OS is not updating your locations.

Try to keep shorter interval and try it will work.

Shelly Pritchard
  • 10,777
  • 4
  • 18
  • 17
0

Did you handle location update from AppDelegate for application state? If not you can update your code with below methods in AppDelegate.

var locationManager = CLLocationManager()

func doBackgroundTask() {
    DispatchQueue.global(qos: .background).async {
        self.beginBackgroundUpdateTask()
        self.StartupdateLocation()
        RunLoop.current.run()
    }
}

func beginBackgroundUpdateTask() {
    backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
        self.endBackgroundUpdateTask()
    })
}
func endBackgroundUpdateTask() {
    UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask)
    self.backgroundUpdateTask = UIBackgroundTaskInvalid
}

func StartupdateLocation() {
    locationManager.startUpdatingLocation()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.distanceFilter = kCLDistanceFilterNone
    locationManager.requestAlwaysAuthorization()
    locationManager.allowsBackgroundLocationUpdates = true
    locationManager.pausesLocationUpdatesAutomatically = false
}

Call the method when application is going in background state.

func applicationWillResignActive(_ application: UIApplication) {
    self.doBackgroundTask()
}

This methods will help you for getting update of location when app is in background. You can take a look at this link if you face any other difficulties. Update location in Background

sazid008
  • 175
  • 2
  • 14