locationServicesEnabled测试在viewDidLoad中被禁用时传递 [英] locationServicesEnabled test passes when they are disabled in viewDidLoad

查看:140
本文介绍了locationServicesEnabled测试在viewDidLoad中被禁用时传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have location services disabled for my application in the settings panel. I run a test in viewDidLoad in my view controller to see if they are enabled:

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

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

This test always passes for some reason. If I try and access location services I get a kCLErrorDenied error for the location manager. What gives?

我使用了错误的测试吗?

Am I using the wrong test?

推荐答案

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

The locationServicesEnabled class method only tests the global setting for Location Services. AFAIK, there's no way to test if your app has explicitly been denied. You'll have to wait for the location request to fail and use the CLLocationManagerDelegate method locationManager:didFailWithError: to do whatever you need. E.g.:

- (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类参考以获取更多选项。

See the documentation on the CLError constants in the CLLocationManager class reference for more options.

这篇关于locationServicesEnabled测试在viewDidLoad中被禁用时传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆