Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out if statUpdatingLocation is active in Swift

I have a location based app running in swift. I am trying to detect if the self.locationManager.startUpdatingLocation() is currently active.

I am struggling to find out how to do this nor can I find much on the internet about it. This I am fairly sure is rather simple to achieve. I don't want to set a BOOL as this needs to be global.

    if CLLocationManager.locationServicesEnabled() && /* START UPDATE LOCATION GOES HERE */ {

        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestAlwaysAuthorization()
        self.locationManager.startUpdatingLocation()

        //self.locationManager.startMonitoringSignificantLocationChanges()

        sender.setTitle("END DAY", forState: UIControlState.Normal)

    } else {


    }
like image 228
Allreadyhome Avatar asked Jun 03 '15 16:06

Allreadyhome


1 Answers

I know this is a late answer now. But this is possibly one solution if you want to know if the locationManager is updating.

There should only be one instance of CLLocationManager in your app. So creating a Singleton is ideal. Then, you should override the methods startUpdatingLocation and stopUpdatingLocation.

(Swift 3)

import CoreLocation

class LocationManager: CLLocationManager {
    var isUpdatingLocation = false

    static let shared = LocationManager()

    override func startUpdatingLocation() {
        super.startUpdatingLocation()

        isUpdatingLocation = true
    }

    override func stopUpdatingLocation() {
        super.stopUpdatingLocation()

        isUpdatingLocation = false
    }
}

Usage:

  if LocationManager.shared.isUpdatingLocation {
    print("Is currently updating location.")
  } else {
    print("Location updates have stopped.")
  }
like image 200
Eric Hodgins Avatar answered Sep 23 '22 01:09

Eric Hodgins