【问题标题】:CLLocationManager not returning location after temporarily disabling in settingsCLLocationManager 在设置中暂时禁用后不返回位置
【发布时间】:2013-08-08 09:59:05
【问题描述】:

我暂时禁用了位置服务和我的应用程序的权限,以便我可以测试一些处理它们不可用的情况的代码。再次打开它们后,现在无法使用以下代码获取我的位置:

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
[locationManager startUpdatingLocation];
CLLocation *currentLocation = locationManager.location;
[locationManager stopUpdatingLocation];

运行此代码后,locationManager.location 等于 nil

我在运行 iOS 6 的 iPad 上运行它。

【问题讨论】:

  • 不清楚您是使用CLLocationManagerDelegate 还是仅使用locationManager.location 属性
  • 我没有使用代理。
  • 我从来没有在没有委托的情况下使用过 CLLocationManager,但我认为如果您关闭定位服务,那么location 属性可能为零。这就是CLLocationManagerDelegate 存在的原因。还给CLLocationManager 时间来检索位置。此外,有时返回的第一个 location 可能是旧的,所以我只需更改该代码

标签: iphone ios objective-c ipad cllocationmanager


【解决方案1】:

为 CLLocationManager 设置委托

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];

试试 CLLocationManager 的委托。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    self.currentLocation = newLocation;            
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    // The location "unknown" error simply means the manager is currently unable to get the location.
    // We can ignore this error for the scenario of getting a single location fix, because we already have a
    // timeout that will stop the location manager to save power.
    if ([error code] != kCLErrorLocationUnknown) {
        [self stopUpdatingLocation:NSLocalizedString(@"Error", @"Error")];
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    • 2014-07-18
    • 1970-01-01
    相关资源
    最近更新 更多