And*_*kha 3 cocoa-touch objective-c core-location ios
当我启动我的应用程序时,我检查当前位置授权状态如下:
- (void)checkCurrentStatus
{
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)
{
[self.locationManager requestWhenInUseAuthorization];
}
else
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied && ![CLLocationManager locationServicesEnabled])
{
[self.locationManager startUpdatingLocation];
}
}
Run Code Online (Sandbox Code Playgroud)
如果启用整体位置服务(对于整个设备)而不是仅仅要求用户许可,则会弹出警报.如果它们被禁用(否则如果条件),那么我需要调用,startUpdatingLocation因为[self.locationManager requestWhenInUseAuthorization];当授权状态为kCLAuthorizationStatusDenied(没有任何条件)时,调用无效.好的,所以我打电话startUpdatingLocation然后提醒弹出窗口说:
启用位置服务以允许"AppName"确定您的位置
好的,我转到整体位置服务的设置.在那之后授权状态变成kCLAuthorizationStatusNotDetermined但是当我打电话requestWhenInUseAuthorization它没有效果!没有弹出窗口,用户没有提示授权位置,状态保持不变,我无法使用位置管理器.我该怎么处理?
来自Apple的CLLocationManager文档 - (void)requestWhenInUseAuthorization
如果当前授权状态不是kCLAuthorizationStatusNotDetermined,则此方法不执行任何操作并且不调用locationManager:didChangeAuthorizationStatus:方法
这就是你需要的:
- (void)requestAlwaysAuthorization
{
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
// If the status is denied or only granted for when in use, display an alert
if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusDenied) {
NSString *title;
title = (status == kCLAuthorizationStatusDenied) ? @"Location services are off" : @"Background location is not enabled";
NSString *message = @"To use background location you must turn on 'Always' in the Location Services Settings";
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Settings", nil];
[alertView show];
}
// The user has not enabled any location services. Request background authorization.
else if (status == kCLAuthorizationStatusNotDetermined) {
[self.locationManager requestAlwaysAuthorization];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
// Send the user to the Settings for this app
NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:settingsURL];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3847 次 |
| 最近记录: |