2011-12-12 51 views
0

我想在我的應用程序中搜索當前位置,但我發現整個位置服務都是不允許的。當位置服務不允許時,我可以訪問核心位置api嗎?

@interface LoginViewController : UIViewController <CLLocationManagerDelegate> 
{ 
    CLLocationManager* locationManager; 
    CLLocation* currentLocation; 
} //header file 

//implementation header file 
(void)loginButtonPressed:(id)aSender 
{ 
    if (locationManager == nil) 
    { 
     locationManager = [[CLLocationManager alloc] init]; 
     locationManager.delegate = self; 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
     locationManager.distanceFilter = kCLDistanceFilterNone; 
    }    

    [locationManager startUpdatingLocation]; 
} 

//implementation CLLocationManagerDelegate 
(void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
    fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"update location"); 
    if(self.currentLocation == nil) 
    { 
     self.currentLocation = newLocation; 
     [locationManager stopUpdatingLocation]; 
    } 
} 

(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
    NSLog(@"fail with error"); 
    [locationManager stopUpdatingLocation]; 
    self.currentLocation = nil; 
} 

locationManager電話startUpdatingLocation(假設位置服務是不允許的),locationManager不能獲得當前位置,並調用didFailWithError: method
在我的情況下,locationManager與iOS5沒有任何聯繫,但撥打didFailWithError: method與iOS4。我不知道我的過錯是什麼。

回答

0

不,您應該始終檢查在啓動任何位置更新之前是否允許位置監視。如果用戶更改了您的應用的偏好設置,則只會收到錯誤信息,而不會更新實際的位置信息。

您應該檢查位置更新是否可用,如果不是,請通知用戶他們需要爲您的應用重新啓用位置服務才能正常工作。

+0

謝謝你的建議。在locationManager開始調用startUpdatingLocation方法之前,它必須檢查CLLocationManager的狀態。因此,條件如果語句更改爲這樣[CLLocationManager locationServicesEnabled] – zeide

相關問題