您的位置:首页 > 移动开发 > IOS开发

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

2016-12-09 11:54 441 查看
判断用户是否开启了定位功能: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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: