我有一个基于位置的应用程序在
swift中运行.我试图检测self.locationManager.startUpdatingLocation()当前是否处于活动状态.
我正在努力寻找如何做到这一点,我也无法在互联网上找到它.我相当肯定这很容易实现.我不想设置BOOL,因为它需要是全局的.
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 { }
解决方法
我知道现在这是一个迟到的答案.但是,如果您想知道locationManager是否正在更新,这可能是一种解决方案.
您的应用中应该只有一个CLLocationManager实例.因此,创建单身人士是理想的选择.然后,您应该重写方法startUpdatingLocation和stopUpdatingLocation.
(斯威夫特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 } }
用法:
if LocationManager.shared.isUpdatingLocation { print("Is currently updating location.") } else { print("Location updates have stopped.") }