gpt4 book ai didi

iphone - 在设置中临时禁用后 CLLocationManager 不返回位置

转载 作者:行者123 更新时间:2023-11-28 22:28:40 27 4
gpt4 key购买 nike

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

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

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

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

最佳答案

为 CLLocationManager 设置委托(delegate)

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

试试 CLLocationManager 的委托(delegate)。

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

关于iphone - 在设置中临时禁用后 CLLocationManager 不返回位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18123083/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com