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