iOS alertview for location permission doesn't pop up

后端 未结 2 554
花落未央
花落未央 2021-01-18 05:05

I just started my project for iOS 8 and i ran in too the problem that i cant get the question to pop up for permission. I added the following to my info.plist



        
2条回答
  •  旧巷少年郎
    2021-01-18 05:28

    Make sure your view controller implements CLLocationManagerDelegate and imports #import CoreLocation/CoreLocation.h

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        [self setLocationManager:[[CLLocationManager alloc] init]];
    
        if([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
            [self.mapview setShowsUserLocation:NO];
            [self.locationManager setDelegate:self];
            [self.locationManager requestWhenInUseAuthorization];
        }
        else{
            //Before iOS 8
            [self.mapview setShowsUserLocation:YES];
        }
    }
    

    Add this method to listen for AuthorizationStatus changes

    -(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
       if (status == kCLAuthorizationStatusDenied) {
           // permission denied
           [self.mapview setShowsUserLocation:NO];
       }
       else if (status == kCLAuthorizationStatusAuthorized) {
           // permission granted
           [self.mapview setShowsUserLocation:YES];
       }
    }
    

    Finally make sure your plist has the following entries as pointed out by Sanne

    NSLocationWhenInUseUsageDescription
    The spirit of stack overflow is coders helping coders
    NSLocationAlwaysUsageDescription
    I have learned more on stack overflow than anything else
    

提交回复
热议问题