我不能在不允许位置服务的情况下访问核心位置api吗?

时间:2011-12-12 04:11:40

标签: core-location

我想在我的应用中搜索当前位置,但我看不到整个位置服务。

@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调用任何内容,而是使用iOS4调用didFailWithError: method。我不知道我的错是什么。

1 个答案:

答案 0 :(得分:0)

不,在开始任何位置更新之前,您应该始终检查是否允许位置监控。如果用户更改了您对应用的偏好,则只会收到错误而不是实际的位置更新。

您应该检查位置更新是否可用,如果没有,请通知用户他们需要为您的应用重新启用位置服务才能使其正常工作。