简体   繁体   English

如何检测用户选择不允许MKMapView for iphone

[英]How to detect user selects Don't Allow for MKMapView for iphone

I have created a application which uses mapview. 我创建了一个使用mapview的应用程序。 For maps I used MKMapKit library. 对于地图,我使用了MKMapKit库。 Everything works fine when user selects "Allow" button on alert window. 当用户在警报窗口中选择“允许”按钮时,一切正常。 But I want to detect when user selects "Don't Allow". 但我想检测用户何时选择“不允许”。 I found a delegate which most of the developers used 我找到了一个大多数开发人员使用的代理

(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

but the delegate does not get called. 但代表没有被召唤。

Maybe I am missing something. 也许我错过了什么。 In my header (.h) file I have implemented MKMapViewDelegate . 在我的标题(.h)文件中,我实现了MKMapViewDelegate Is there anything else I need to do? 还有什么我需要做的吗?

Do I need to add some extra classes like CLLocationManager or else. 我是否需要添加一些额外的类,如CLLocationManager或其他。

Thanks, 谢谢,

In order to monitor the changes in the authorization status for the location services you need to implement the CLLocationManagerDelegate method locationManager:didChangeAuthorizationStatus: obtaining something like 为了监视位置服务的授权状态的变化,您需要实现CLLocationManagerDelegate方法locationManager:didChangeAuthorizationStatus:获取类似的东西

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {

    if (status == kCLAuthorizationStatusDenied) {
        // permission denied
    }
    else if (status == kCLAuthorizationStatusAuthorized) {
        // permission granted
    }
}

For a complete list of the possible authorization statuses and their description you can check out the official documentation of CLAuthorizationStatus . 有关可能的授权状态及其描述的完整列表,您可以查看CLAuthorizationStatus的官方文档。

EDIT 编辑

You probably have already your instance of CLLocationManager , let's call it locationManager . 您可能已经有了CLLocationManager的实例,我们称之为locationManager Then in order to implement your delegate you conform your class to the CLLocationManagerDelegate protocol (you can declare it in the header of class -- this is not mandatory but it will provide you some static checking facilities) and assign it to the delegate property of locationManager like follows: 然后,为了实现您的委托,您将您的类符合CLLocationManagerDelegate协议(您可以在类的标头中声明它 - 这不是强制性的,但它将为您提供一些静态检查工具)并将其分配给locationManagerdelegate属性如下:

locationManager.delegate = self; //assuming that self is gonna be the delegate

If you did everything as explained your controller will be called at every authorization change, as stated by the documentation: 如果按照说明执行了所有操作,则每次授权更改时都会调用控制器,如文档所述:

this method is called whenever the application's ability to use location services changes. 只要应用程序使用位置服务的能力发生变化,就会调用此方法。

Can you try this: 你能试试这个:

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
    }

}

// Wheneever you check //无论你检查

- (IBAction)showMapBtnPressed:(id)sender {

    if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)
    {
        NSLog(@"Dont allow");

    }else
    {
        NSLog(@" allow");
    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM