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

时间秒杀一切 提交于 2019-12-04 07:26:27

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.

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


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