临时禁用设置后,CLLocationManager不返回位置

时间:2013-08-08 09:59:06

标签: iphone ios objective-c ipad cllocationmanager

我暂时禁用了位置服务和我的应用程序的权限,这样我就可以测试一些处理无法使用的方案的代码。再次打开它时,现在无法使用以下代码获取我的位置:

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

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

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

1 个答案:

答案 0 :(得分:2)

设置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")];
    }
}