【发布时间】:2016-01-23 08:09:49
【问题描述】:
我是 ios 新手,我想知道当我在下面使用它时如何通过编程启用位置服务....
当我使用这个标签来显示位置服务被禁用时和一个启用按钮(绿色)来启用位置服务。但是如何以编程方式进行。我将如何了解位置服务被禁用以及按钮将如何启用此功能。请帮帮我。
【问题讨论】:
-
提问前请阅读规则
-
Zoho,你能把链接给我吗
标签: ios objective-c
我是 ios 新手,我想知道当我在下面使用它时如何通过编程启用位置服务....
当我使用这个标签来显示位置服务被禁用时和一个启用按钮(绿色)来启用位置服务。但是如何以编程方式进行。我将如何了解位置服务被禁用以及按钮将如何启用此功能。请帮帮我。
【问题讨论】:
标签: ios objective-c
您可以使用 CLLocationManager 的 locationServicesEnabled 属性来检查系统范围/整体的可用性。使用您的 CLLocationManagerDelegate 的 locationManager: didFailWithError: 方法并检查 kCLErrorDenied 错误以查看用户是否拒绝了位置服务。
if([CLLocationManager locationServicesEnabled]){
NSLog(@"Location Services Enabled");
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied){
[[[UIAlertView alloc] initWithTitle:@"App Permission Denied"
message:@"To re-enable, please go to Settings and turn on Location Service for this app."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil]show];
}
}
选择 2
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"%@",error);
}
如果您的应用禁用了定位服务,那么它会给您错误
Error Domain=kCLErrorDomain Code=1 "操作无法完成。(kCLErrorDomain error 1.)"
更新
如果用户拒绝了权限,我们无法以编程方式启用,用户可以手动启用位置服务,更多reference
【讨论】: