gpt4 book ai didi

iphone - locationServicesEnabled 在 vi​​ewDidLoad 中禁用时测试通过

转载 作者:行者123 更新时间:2023-12-03 18:33:22 25 4
gpt4 key购买 nike

我在设置面板中为我的应用程序禁用了位置服务。我在 View Controller 中的 viewDidLoad 中运行测试以查看它们是否已启用:

if([CLLocationManager locationServicesEnabled]) {
//Do something now
}

由于某种原因,该测试总是通过。如果我尝试访问位置服务,我会收到位置管理器的 kCLErrorDenied 错误。给出了什么?

我使用了错误的测试吗?

最佳答案

locationServicesEnabled 类方法仅测试位置服务的全局设置。 AFAIK,无法测试您的应用程序是否已被明确拒绝。您必须等待位置请求失败并使用 CLLocationManagerDelegate 方法 locationManager:didFailWithError: 来执行您需要的任何操作。例如:

- (void)locationManager: (CLLocationManager *)manager
didFailWithError: (NSError *)error {

NSString *errorString;
[manager stopUpdatingLocation];
NSLog(@"Error: %@",[error localizedDescription]);
switch([error code]) {
case kCLErrorDenied:
//Access denied by user
errorString = @"Access to Location Services denied by user";
//Do something...
break;
case kCLErrorLocationUnknown:
//Probably temporary...
errorString = @"Location data unavailable";
//Do something else...
break;
default:
errorString = @"An unknown error has occurred";
break;
}
}

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}

请参阅 CLLocationManager class reference 中有关 CLError 常量的文档。了解更多选项。

关于iphone - locationServicesEnabled 在 vi​​ewDidLoad 中禁用时测试通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3401757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com