2015-10-12 33 views
2

我目前使用CLLocationManager来始终跟踪地理围栏,即使应用程序在后台也是如此。当位置服务启用/禁用时,我似乎无法找到一种方法来侦听。iOS位置服务应用程序关闭时启用/禁用事件

是否有可能侦听位置服务启用/禁用事件,或者当应用程序关闭时针对特定应用程序启用/禁用位置?

请注意我正在使用Xamarin,但Objective-C代码很好。

public class LocationManager 
{ 
    protected CLLocationManager locationManager; 

    public LocationManger() 
    { 
     this.locationManager = new CLLocationManger(); 

     if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0)) 
     { 
      locationManager.RequestAlwaysAuthorization(); 
     } 


     // ... get array of CLCircularRegion and start listening to each 

     // locationManager events... 
     locationManager.RegionEntered += (sender, e) => { /*stuff*/ }; 
     locationManager.RegionLeft += (sender, e) => { /*stuff*/ }; 
     locationManager.DidDetermineState += (sender, e) => { /*stuff*/ }; 
     //locationaManager.SomeSortOfLocationServiceEnableDisableEvent += (sender, e) => { /*stuff*/ }; 
    } 
} 

回答

4

调用的类方法[CLLocationManager locationServicesEnabled]返回一个BOOL指示位置服务是否启用。

如果用户禁用位置服务,locationManager:didChangeAuthorizationStatus:将在CLLocationManagerDelegate上调用。

因此,如果您有一个类符合CLLocationManagerDelegate并执行locationManager:didChangeAuthorizationStatus:,您应该能够处理用户的禁用事件。

+0

是的,但没有告诉我何时位置服务发生变化。 –

+0

我用处理授权状态更改的委托方法更新了我的答案。这有帮助吗? – Daniel

+0

这似乎并不完美,但看起来它是iOS所能提供的最佳选择。谢谢丹尼尔。杰瑞德,没问题, –

相关问题