2011-09-02 19 views
7
location = [[CLLocationManager alloc] init]; 
    location.desiredAccuracy = kCLLocationAccuracyBestForNavigation ; 
    location.distanceFilter = 10 ; 
    location.delegate=self; 



    locationEnabledBool = [CLLocationManager locationServicesEnabled]; 

    if (locationEnabledBool ==NO) { 
     UIAlertView *locationAlert = [[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]; 
     [locationAlert show]; 
     [locationAlert release]; 

    } 
    else 
     [location startUpdatingLocation]; 

locationEnabledBool值始終爲YES,獨立的位置服務是否被啓用。任何身體可以幫助嗎?檢查locationServicesEnabled總是返回YES,無論手動撥動開關來決定,如果位置服務已啓用

回答

11

代替

if (locationEnabledBool == NO) { 
    //give error message 
} 

嘗試

if ([CLLocationManager locationServicesEnabled] ==NO || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) { 
    //give error message 
} 

我發現這個鏈接。

Detecting whether location services are enabled for my app

+0

'if([CLLocationManager locationServicesEnabled] == NO && [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) { //提供錯誤信息 }'在第一個[括號]中有一個小錯字,而& – kalafun

0

當您測試此代碼時,您需要確保您在設備上進行測試,而不僅僅是使用iOS模擬器。

另外,我建議,在該設備上設置你仔細檢查,以確保位置服務,對設置的第一頁上,說:

+1

感謝您的回覆。我在設備上運行代碼並檢查位置服務在設置中顯示爲OFF。而不是if(locationEnabledBool == NO)if(locationEnabledBool == NO ||([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied))爲我工作。 – alekhine

相關問題