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。我不知道我的過錯是什麼。
謝謝你的建議。在locationManager開始調用startUpdatingLocation方法之前,它必須檢查CLLocationManager的狀態。因此,條件如果語句更改爲這樣[CLLocationManager locationServicesEnabled] – zeide