判断用户是否开启了定位功能: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];
本文介绍如何在iOS应用中检查并启用定位服务权限。通过代码示例展示了判断定位服务状态的方法,以及当权限被拒绝时如何提示用户进行设置调整。
1万+

被折叠的 条评论
为什么被折叠?



