判断应用是否允许定位当前位置

来源:互联网 发布:知乎 乾隆 书法 编辑:程序博客网 时间:2024/05/22 11:37

判断用户是否开启了定位功能:if ([CLLocationManager locationServicesEnabled] &&

        ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized
         || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)) {
            //定位功能可用,开始定位
            _locationManger = [[CLLocationManager alloc] init];
            _locationManger.delegate = self;
            [_locationManger startUpdatingLocation];
            
        } else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
            NSLog(@"不可用");

        }


如果不可用,则提醒用户前往设置中去设置开启定位,并为用户设置跳转路径:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"开启定位" message:@"The calendar permission was not authorized. Please enable it in Settings to continue." preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *setting = [UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSURL *appSettings = [NSURL URLWithString:[NSString stringWithFormat:@"%@",UIApplicationOpenSettingsURLString]];
        [UIApplication.sharedApplication openURL:appSettings];
        
    }];
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertController addAction:setting];
    [alertController addAction:cancle];
    [self presentViewController:alertController animated:YES completion:nil];



1 0
原创粉丝点击