gpt4 book ai didi

ios8 - 为什么 CLLocationManager startUpdatingLocation 在 iOS8 中不触发?

转载 作者:行者123 更新时间:2023-12-05 00:39:11 26 4
gpt4 key购买 nike

我正在使用 XCode 6 附带的模拟器测试 iOS 8.0 中的现有位置功能。我无法获得 CLLocationManager.startUpdatingLocation触发 CLLocationManagerDelegate.locationManager:didUpdateLocations或者
CLLocationManagerDelegate.locationManager:didFailWithError .

我没有像在 iOS 7.0.3 模拟器上那样收到“我的应用程序”想要使用您当前位置的警报。我使用 map 应用程序来验证模拟位置设置。

这在 iOS 7.0.3 模拟器中运行良好。

我哪里错了?

最佳答案

iOS 8 要求您调用 CLLocationManager.requestWhenInUseAuthorizationCLLocationManager.requestAlwaysAuthorization在您调用之前 CLLocationManager.startUpdatingLocation .

@interface MyViewController : UIViewController <CLLocationManagerDelegate>

@property CLLocationManager *locationManager;

@end
requestWhenInUseAuthorizationrequestAlwaysAuthorization异步运行,因此您要确保您的 CLLocationManager 对象在用户可以响应警报之前未被清除。
- (void) viewDidAppear:(BOOL)animated {
if ([CLLocationManager locationServicesEnabled]) {
self.locationManager = [CLLocationManager new];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

SEL selector = NSSelectorFromString(@"requestWhenInUseAuthorization");
if ([self.locationManager respondsToSelector:selector]) {
[self.locationManager requestWhenInUseAuthorization];
} else {
[self.locationManager startUpdatingLocation];
}
} else {
...
}
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
[self.locationManager startUpdatingLocation];
} else if (status == kCLAuthorizationStatusAuthorized) {
// iOS 7 will redundantly call this line.
[self.locationManager startUpdatingLocation];
} else if (status > kCLAuthorizationStatusNotDetermined) {
...
}
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
...
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
...
}

当您实现 CLLocationManagerDelegate您现在需要实现 locationManager:didChangeAuthorizationStatus:方法也一样。在此方法中,您可以检查用户是否已授予应用程序权限,并采取相应措施。

[CLLocationManager authorizationStatus] = nil 然后 locationManager:didChangeAuthorizationStatus: 当authorizationStatus 设置为 时将被调用 kCLAuthorizationStatusNotDetermined 当用户从警报对话框中进行选择时再次出现。

关于ios8 - 为什么 CLLocationManager startUpdatingLocation 在 iOS8 中不触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25933315/

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