gpt4 book ai didi

ios - CLLocation didupdatetolocation 未调用

转载 作者:可可西里 更新时间:2023-11-01 03:39:12 24 4
gpt4 key购买 nike

花了几天时间试图解决这个问题,但找不到任何有效的解决方案。我检查了 stackoverflow 上的所有帖子并尝试了他们所有的解决方案,但似乎没有任何效果。我还尝试了 CLLocation 的苹果测试项目,它对我来说效果很好。我使用了苹果测试项目中的点点滴滴

https://developer.apple.com/library/ios/samplecode/LocateMe/Listings/README_md.html

但是我的代码根本不起作用。 DidupdateToLocation 永远不会被调用。

这是我的代码(在 viewDidLoad 中)

_locationManager = [[CLLocationManager alloc] init];

self.locationManager.delegate = self;



// This is the most important property to set for the manager. It ultimately determines how the manager will

// attempt to acquire location and thus, the amount of power that will be consumed.

self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;



// Once configured, the location manager must be "started"

//

// for iOS 8, specific user level permission is required,

// "when-in-use" authorization grants access to the user's location

//

// important: be sure to include NSLocationWhenInUseUsageDescription along with its

// explanation string in your Info.plist or startUpdatingLocation will not work.

//

if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {

[self.locationManager requestWhenInUseAuthorization];

}

[self.locationManager startUpdatingLocation];



[self performSelector:@selector(stopUpdatingLocationWithMessage:)

withObject:@"Timed Out"

afterDelay:30];

我已检查以确保 locationServicesEnabled 已启用。

我已将 NSLoationWhenInUseUsageDescription 属性添加到 info.plist 文件中。我是否需要添加任何其他属性或启用任何服务??

看在上帝的份上,我无法弄清楚我做错了什么。有人可以帮我解决这个问题吗?

最佳答案

在 iOS8 上收到 didChangeAuthorization Status 回调后,您必须调用 startUpdatingLocation。

//iOS 8 API change
if([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
[self.locationManager requestWhenInUseAuthorization];
}else{
[self.locationManager startUpdatingLocation];
}

实现这个委托(delegate)方法:

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusNotDetermined:
case kCLAuthorizationStatusRestricted:
case kCLAuthorizationStatusDenied:
{
// do some error handling
}
break;
default:{
[self.locationManager startUpdatingLocation];
}
break;
}
}

关于ios - CLLocation didupdatetolocation 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26218575/

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