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

问题描述:

使用CLLocationManager,我可以使用下面的代码来确定我是否可以访问设备上的位置服务。这是所有应用程序的主设置,可以打开和关闭。当用户拒绝访问位置服务时,您如何确定使用CLLocationManager?

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

但用户可以拒绝访问单个应用程序,并以不执行代码中使用到的位置经理,我需要知道,如果用户批准访问位置服务这个特定的应用程序。我看到有一次有一个名为locationServicesApproved的属性,它显示它会指示用户是否批准在此应用中访问位置服务。 http://trailsinthesand.com/apple-removes-notifications-from-iphone-sdk-beta-4/

看来,有没有办法确定用户是否批准访问定位服务,但是,这似乎是在SDK一个大洞:但它是在2008年

源中删除。

SDK中的这个功能在其他地方吗?我能做些什么来确定用户是否已批准访问当前应用的位置服务?

+1

看来,你必须等待对于locationManager:didFailWithError:将被调用,并且错误代码将指向CLError中的值。H。值为kCLErrorLocationUnknown,kCLErrorDenied,kCLErrorNetwork和kCLErrorHeadingFailure。 看起来第二个值是我应该检查用户是否拒绝对位置服务的访问。 – Brennan 2010-03-13 20:49:23

+0

回答你自己的问题并接受你自己的回答是可以的(你没有得到代表,但它会帮助人们以后搜索)。另外,问题在列表中显示为已回答。 – 2010-03-13 21:56:30

我在上面的问题的评论中回答了我自己的问题。

答案(从上面的注释复制):

看来,你必须等待的LocationManager:didFailWithError:被称为和错误代码将指向值CLError.h。值为kCLErrorLocationUnknown,kCLErrorDenied,kCLErrorNetwork和kCLErrorHeadingFailure。看起来第二个值是我应该检查用户是否拒绝对位置服务的访问。

从这个了这么回答:locationServicesEnabled test passes when they are disabled in viewDidLoad

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

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

+0

尽管此链接可能会回答问题,但最好在此处包含答案的重要部分,并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 – gnat 2012-11-18 12:11:19

+1

我更新了我的答案。 – testing 2012-11-19 15:33:27

由于iOS 4.2的,你可以使用全局法+ (CLAuthorizationStatus)authorizationStatusCLLocationManager

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