Check if location services are enabled

前端 未结 9 1687
情话喂你
情话喂你 2020-12-02 08:34

I\'ve been doing some research about CoreLocation. Recently, I encountered a problem that has been covered elsewhere, but in Objective C, and for iOS 8.

I feel kinda

9条回答
  •  伪装坚强ぢ
    2020-12-02 09:18

    In objective-c

    you should track user already denied or not determined then ask for permission or sent user to Setting app.

    -(void)askEnableLocationService
    {
       BOOL showAlertSetting = false;
       BOOL showInitLocation = false;
    
       if ([CLLocationManager locationServicesEnabled]) {
    
          switch ([CLLocationManager authorizationStatus]) {
            case kCLAuthorizationStatusDenied:
                showAlertSetting = true;
                NSLog(@"HH: kCLAuthorizationStatusDenied");
                break;
            case kCLAuthorizationStatusRestricted:
                showAlertSetting = true;
                NSLog(@"HH: kCLAuthorizationStatusRestricted");
                break;
            case kCLAuthorizationStatusAuthorizedAlways:
                showInitLocation = true;
                NSLog(@"HH: kCLAuthorizationStatusAuthorizedAlways");
                break;
            case kCLAuthorizationStatusAuthorizedWhenInUse:
                showInitLocation = true;
                NSLog(@"HH: kCLAuthorizationStatusAuthorizedWhenInUse");
                break;
            case kCLAuthorizationStatusNotDetermined:
                showInitLocation = true;
                NSLog(@"HH: kCLAuthorizationStatusNotDetermined");
                break;
            default:
                break;
          }
       } else {
    
          showAlertSetting = true;
          NSLog(@"HH: locationServicesDisabled");
      }
    
       if (showAlertSetting) {
           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"Please enable location service for this app in ALLOW LOCATION ACCESS: Always, Go to Setting?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Open Setting", nil];
           alertView.tag = 199;
           [alertView show];
       }
       if (showInitLocation) {
           [self initLocationManager];
       }
    
    }
    

    Implement alertView Delegate then sent user to enable location service if already deny by user.

    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
    
       if (alertView.tag == 199) {
           if (buttonIndex == 1) {
               [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
           }
           return;
       }
    }
    

    Init Location Manager

    -(void)initLocationManager{
       self.locationManager = [[CLLocationManager alloc] init];
       if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
           [self.locationManager requestAlwaysAuthorization];
       }
    }
    

    Please note kCLAuthorizationStatusAuthorizedAlways and kCLAuthorizationStatusAuthorizedWhenInUse is difference.

提交回复
热议问题