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

血红的双手。 提交于 2019-12-06 03:47:14

问题


I have created a application which uses mapview. For maps I used MKMapKit library. 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. Is there anything else I need to do?

Do I need to add some extra classes like CLLocationManager or else.

Thanks,


回答1:


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

-(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.

EDIT

You probably have already your instance of CLLocationManager, let's call it 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:

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.




回答2:


Can you try this:

if(![CLLocationManager locationServicesEnabled])
{
    // alert location services denied
}



回答3:


// 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");
    }


}


来源:https://stackoverflow.com/questions/13910748/how-to-detect-user-selects-dont-allow-for-mkmapview-for-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!