自从新的iOS 8测试版发布以来,我无法成功获取用户的位置.在更新到iOS 8之前,我没有任何问题,但现在它总是返回0.000000作为当前纬度和经度.这只是新版本中的一个错误吗?我的代码如下:
//from the .h file
@interface MasterViewController : PFQueryTableViewController<CLLocationManagerDelegate,UITextFieldDelegate,UISearchBarDelegate,UISearchdisplayDelegate> {
}
@property (nonatomic,strong) CLLocationManager *locationManager;
//from the .m file
@synthesize locationManager = _locationManager;
- (void)viewDidLoad {
[super viewDidLoad];
[self.locationManager startUpdatingLocation];
}
- (CLLocationManager *)locationManager {
if (_locationManager != nil) {
return _locationManager;
}
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
return _locationManager;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdatetoLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
}
UPDATE
这个问题已得到解答(Location Services not working in iOS 8).对于仍在努力解决这个问题的人来说,为了保持与iOS 7的向后兼容性,我使用了以下代码:
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { }
解决方法
如您的更新/评论中所述,iOS8要求您在Info.plist中使用requestAlwaysAuthorization或requestWhenInUseAuthorization以及新的NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription键.
但是你的代码还有其他错误:
- (void)locationManager:(CLLocationManager *)manager
didUpdatetoLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
它已在iOS6中弃用,您现在应该使用这个新的委托方法:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
然后,您可以使用以下内容获取最新位置:
CLLocation *newLocation = [locations lastObject];