【问题标题】:Can I access to core location api when location service is not allowed?不允许位置服务时,我可以访问核心位置 api 吗?
【发布时间】:2011-12-12 04:11:39
【问题描述】:

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

@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。我不知道是我的错。

【问题讨论】:

    标签: core-location


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的建议。在 locationManager 开始调用 startUpdatingLocation 方法之前,它必须检查 CLLocationManager 的状态。所以 if 语句中的条件改为这样 [CLLocationManager locationServicesEnabled]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多