iOS 8 beta - 位置管理器未注册用户位置

JKo*_*oko 7 core-location mapkit ios ios8

自从新的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 {
}
Run Code Online (Sandbox Code Playgroud)

更新 此问题已得到解答(位置服务在iOS 8中不起作用).对于仍在努力解决这个问题的人来说,为了保持与iOS 7的向后兼容性,我使用了以下代码:

if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])    { } 
Run Code Online (Sandbox Code Playgroud)

Erw*_*wan 2

正如您的更新/评论中提到的,iOS8 要求您使用requestAlwaysAuthorizationrequestWhenInUseAuthorization以及新的NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription键入Info.plist

但是您的代码中还有其他错误:

- (void)locationManager:(CLLocationManager *)manager
        didUpdateToLocation:(CLLocation *)newLocation
               fromLocation:(CLLocation *)oldLocation
Run Code Online (Sandbox Code Playgroud)

它在 iOS6 中已被弃用,您现在应该使用这个新的委托方法:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
Run Code Online (Sandbox Code Playgroud)

然后,您可以通过以下方式获取最新位置:

CLLocation *newLocation = [locations lastObject];
Run Code Online (Sandbox Code Playgroud)