2010-03-13 50 views
1

使用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

回答

0

我在上面的問題的評論中回答了我自己的問題。

答案(從上面的註釋複製):

看來,你必須等待的LocationManager:didFailWithError:被稱爲和錯誤代碼將指向值CLError.h。值爲kCLErrorLocationUnknown,kCLErrorDenied,kCLErrorNetwork和kCLErrorHeadingFailure。看起來第二個值是我應該檢查用戶是否拒絕對位置服務的訪問。

4

從這個了這麼回答: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

0

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

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