0

I am developing ios app using Swift4 and in that, I need to fetch user location continuously after few seconds in all states i.e. background, foreground or even app is killed. I tried a lot of codes available but none seems work.

Capture location in all states

For app delegate :

if ((launchOptions?[UIApplication.LaunchOptionsKey.location]) != nil) {
            print ("abhishek testing termination")
            fromTerminated = true

            locationManager.requestAlwaysAuthorization()
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
            locationManager.allowsBackgroundLocationUpdates = true
            locationManager.startUpdatingLocation()
            locationManager.startMonitoringSignificantLocationChanges() //THIS IS WHERE THE MAGIC HAPPENS
        }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if(fromTerminated)
        {
            let newlocation : CLLocation = locations.last!
            let locValue:CLLocationCoordinate2D = newlocation.coordinate
           // let theaccuracy : CLLocationAccuracy = newlocation.horizontalAccuracy

            print ("background location\(locValue.latitude)")
            print ("terminated location\(locValue.longitude)")
          //  self.postdatatoserver()
        }

    }
Muneeb Ali
  • 472
  • 6
  • 14

0 Answers0