简体   繁体   English

当用户拒绝访问位置服务时,如何使用CLLocationManager确定?

[英]How do you determine with CLLocationManager when a user denies access to location services?

With CLLocationManager I can use the following code to determine if I can access location services on the device. 使用CLLocationManager,我可以使用以下代码来确定是否可以访问设备上的位置服务。 This is the master setting for all apps and can be turned on and off. 这是所有应用程序的主设置,可以打开和关闭。

if (self.locationManager.locationServicesEnabled) {
    [self.locationManager startUpdatingLocation];
}

But a user can deny access to an individual app and in order to not execute the code to use the location manager I need to know if the user approved access to location services for this specific app. 但是,用户可以拒绝访问单个应用程序,并且为了不执行代码以使用位置管理器,我需要知道用户是否批准访问此特定应用程序的位置服务。 I saw that at one point there was a property called locationServicesApproved which appears it would indicate if the user approved access to location services in this app. 我看到有一个名为locationServicesApproved的属性,它似乎表明用户是否批准访问此应用程序中的位置服务。 But it was removed in 2008. 但它在2008年被删除。

Source: http://trailsinthesand.com/apple-removes-notifications-from-iphone-sdk-beta-4/ 资料来源: http//trailsinthesand.com/apple-removes-notifications-from-iphone-sdk-beta-4/

It appears that there is no way to determine if the user approved access to location services but that seems to be a big hole in the SDK. 似乎无法确定用户是否批准访问位置服务,但这似乎是SDK中的一个大漏洞。

Is this feature in the SDK elsewhere? SDK中的此功能是否在其他地方? What can I do to determine if the user has approved access to location services for the current app? 我该怎么做才能确定用户是否已批准访问当前应用的位置服务?

Taken from this SO answer: locationServicesEnabled test passes when they are disabled in viewDidLoad 从这个SO答案中获取: locationServicesEnabled测试在viewDidLoad中被禁用时传递

The locationServicesEnabled class method only tests the global setting for Location Services. locationServicesEnabled类方法仅测试位置服务的全局设置。 AFAIK, there's no way to test if your app has explicitly been denied. AFAIK,没有办法测试你的应用是否被明确拒绝。 You'll have to wait for the location request to fail and use the CLLocationManagerDelegate method locationManager:didFailWithError: to do whatever you need. 您将不得不等待位置请求失败并使用CLLocationManagerDelegate方法locationManager:didFailWithError:来执行您需要的任何操作。 Eg 例如

- (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];
}

See the documentation on the CLError constants in the CLLocationManager class reference for more options. 有关更多选项,请参阅CLLocationManager类参考中有关CLError常量的文档。

I answered my own question in the comment to the question above. 我在上述问题的评论中回答了我自己的问题。

The answer (copied from the comment above): 答案(从上面的评论中复制):

It appears that you must wait for locationManager:didFailWithError: to be called and the error code will point to values in CLError.h. 您似乎必须等待调用locationManager:didFailWithError:并且错误代码将指向CLError.h中的值。 Values are kCLErrorLocationUnknown, kCLErrorDenied, kCLErrorNetwork, and kCLErrorHeadingFailure. 值为kCLErrorLocationUnknown,kCLErrorDenied,kCLErrorNetwork和kCLErrorHeadingFailure。 It appears that the second value is what I should check to see if the user denied access to location services. 似乎第二个值是我应该检查以查看用户是否拒绝访问位置服务。

Since iOS 4.2, you can use the global method + (CLAuthorizationStatus)authorizationStatus from CLLocationManager : 从iOS 4.2开始,您可以使用CLLocationManager的全局方法+ (CLAuthorizationStatus)authorizationStatus

if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
    //User denied access to location service for this app
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM