在 iOS Objective C 中处理位置权限

Handling Location Permission in iOS Objective C

我想在用户 AllowDisallow 位置权限后检查用户位置服务是否启用,我在 viewdidload 方法中检查了权限,但它出现在用户设置权限之前,我想在用户从设备中的权限弹出窗口设置位置权限(允许或不允许)后 运行 此代码。 Objective c

-(void)viewDidLoad

if(status==kCLAuthorizationStatusAuthorizedWhenInUse) {
        float latitude;
        float longitude;
        CLLocationCoordinate2D coordinate = [self myLocation];
        latitude = coordinate.latitude;
        longitude = coordinate.longitude;
        status = [CLLocationManager authorizationStatus];

    CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

    CLGeocoder*myGeocoder = [[CLGeocoder alloc] init];

    [myGeocoder
     reverseGeocodeLocation:location
     completionHandler:^(NSArray *placemarks, NSError *error) {

         if (error == nil && placemarks.count > 0){

             CLPlacemark *placemark = placemarks[0];
             if ([placemark.locality isEqualToString:@"Mumbai"]) {
                 NSLog(@"Location is Mumbai");
             }
             else{
                 if (placemark.locality) {
                     alert=[UIAlertController alertControllerWithTitle:nil message:[NSString stringWithFormat:@"This app is not available @%@",placemark.locality] preferredStyle:UIAlertControllerStyleAlert];
                     [self presentViewController:alert animated:YES completion:nil];
                 }
                 else{
                     alert=[UIAlertController alertControllerWithTitle:nil message:@"This app is not available at your location" preferredStyle:UIAlertControllerStyleAlert];
                     [self presentViewController:alert animated:YES completion:nil];
                 }
             }


         }
         else if (error == nil && placemarks.count == 0){
             NSLog(@"No results were returned.");
         }
         else if (error != nil) {
             NSLog(@"An error occurred = %@", error);
         }
     }];
}
else{

    alert=[UIAlertController alertControllerWithTitle:@"Location Permission" message:@"To re-enable, please go to Settings and turn on Location Service for this app." preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *Retry=[UIAlertAction actionWithTitle:@"Retry" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
       [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
        exit(0);
    }];
    [alert addAction:Retry];
    [self presentViewController:alert animated:YES completion:nil];
}

首先包括代表:

CLLocationManagerDelegate

正在声明 属性:

@property (strong, nonatomic) CLLocationManager *locationManager;

在视图中加载添加了以下内容:

if( _locationManager == nil )
    {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [_locationManager startUpdatingLocation];
    }

[self checkLocationAccess];

以及 checkLocationAccess 方法:

- (void)checkLocationAccess {
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    switch (status) {

    // custom methods after each case

        case kCLAuthorizationStatusDenied:
            [self allowLocationAccess]; // custom method
            break;
        case kCLAuthorizationStatusRestricted:
            [self allowLocationAccess]; // custom method
            break;
        case kCLAuthorizationStatusNotDetermined:
            break;
        case kCLAuthorizationStatusAuthorizedAlways:
            break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:
            break;
    }
}

观察者方法:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
        NSLog(@"allowed"); // allowed
    }
    else if (status == kCLAuthorizationStatusDenied) {
        NSLog(@"denied"); // denied
    } 
}