如何检测用户选择不允许MKMapView for iphone
我创建了一个使用mapview的应用程序。 对于地图,我使用了MKMapKit
库。 当用户在警报窗口中选择“允许”按钮时,一切正常。 但我想检测用户何时选择“不允许”。 我找到了一个大多数开发人员使用的代理
(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
但代表没有被召唤。
也许我错过了什么。 在我的标题(.h)文件中,我实现了MKMapViewDelegate
。 还有什么我需要做的吗?
我是否需要添加一些额外的类,如CLLocationManager
或其他。
谢谢,
为了监视位置服务的授权状态的变化,您需要实现CLLocationManagerDelegate
方法locationManager:didChangeAuthorizationStatus:
获取类似的东西
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { if (status == kCLAuthorizationStatusDenied) { // permission denied } else if (status == kCLAuthorizationStatusAuthorized) { // permission granted } }
有关可能的授权状态及其描述的完整列表,您可以查看CLAuthorizationStatus的官方文档。
编辑
您可能已经有了CLLocationManager
的实例,我们称之为locationManager
。 然后,为了实现您的委托,您将您的类符合CLLocationManagerDelegate
协议(您可以在类的标头中声明它 – 这不是强制性的,但它将为您提供一些静态检查工具)并将其分配给locationManager
的delegate
属性如下:
locationManager.delegate = self; //assuming that self is gonna be the delegate
如果按照说明执行了所有操作,则每次授权更改时都会调用控制器,如文档所述:
只要应用程序使用位置服务的能力发生变化,就会调用此方法。
你能试试这个:
if(![CLLocationManager locationServicesEnabled]) { // alert location services denied }
// in appdelegate put thecode -(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { if (status == kCLAuthorizationStatusDenied) { //location denied, handle accordingly NSLog(@"Dont allow"); } else if (status == kCLAuthorizationStatusAuthorized) { NSLog(@"Allow"); //hooray! begin startTracking } }
//无论你检查
- (IBAction)showMapBtnPressed:(id)sender { if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) { NSLog(@"Dont allow"); }else { NSLog(@" allow"); } }