2011-02-06 42 views
6

如何檢查用戶是否已關閉位置服務?如何檢查位置服務是否打開?

爲了使用我的應用程序,我可以提示他/她打開它。

謝謝!

+0

你要問這對http://apple.stackexchange.com/可能是在那裏你會得到答案快。 – 2011-02-06 15:59:24

回答

13

CLLocationManager提供類方法來確定位置服務的可用性:

- (BOOL)locationServicesEnabled (for < iOS 4.0)

+ (BOOL)locationServicesEnabled (for iOS 4.0 and greater)

+ (CLAuthorizationStatus)authorizationStatus (for iOS 4.2+)

(和其他人,看文檔)

+0

這個屬性是我一直在尋找!謝謝! =) – nosuic 2011-02-06 16:20:10

3

如果您的應用程序絕對不能運行沒有定位服務,那麼您可以使位置服務的安裝/運行應用程序的使用應用程序的Info.plist的要求。您可以通過將UIDeviceCapabilities關鍵字添加到應用程序的Info.plist中,併爲其添加相應的「location-services」值減去引號。

通過這種方式配置Info.plist,如果定位服務關閉,或者設備處於飛行模式,或者其他任何設置阻止在設備上使用定位服務,iOS會提示用戶轉向在應用程序打開時位置服務。

編輯:簡短的實驗似乎表明,iOS在這種情況下不提示用戶,所以這不會是一個很好的解決方案。

有關更多信息,請參閱Apple開發人員文檔的信息屬性列表關鍵參考部分。

+0

非常感謝您的信息! – nosuic 2011-02-06 16:19:27

1

使用以下一段代碼...

if (![CLLocationManager locationServicesEnabled]) { 


    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Service Disabled" 
                message:@"To re-enable, please go to Settings and turn on Location Service for this app." 
                delegate:nil 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 

} 
0

使用下面的代碼,這將工作,即使在iOS的8

if([CLLocationManager locationServicesEnabled]&& 
    [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied) 
{ 
    //...Location service is enabled 
} 
else 
{ 
//...Location service is disabled 
} 
相關問題