花了几天时间试图解决这个问题但无法找到任何有效的解决方案.我已经检查了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上收到didChangeAuthorizationStatus回调后,你必须调用startUpdatingLocation.
//iOS 8 API change
if([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
[self.locationManager requestWhenInUseAuthorization];
}else{
[self.locationManager startUpdatingLocation];
}
实现这个委托方法:
-(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;
}
}