核心位置不能在iOS 8中工作

我试图在iOS 8中使用显着的更改位置监视,但didUpdateLocations方法从不调用。 以下是设置位置pipe理器的代码:

- (void)viewDidLoad { [super viewDidLoad]; CLLocationManager *locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; [locationManager requestWhenInUseAuthorization]; [locationManager startMonitoringSignificantLocationChanges]; } 

尽pipe在调用requestWhenInUseAuthorization ,没有任何东西会popup来要求用户授权。 我已经设置NSLocationWhenInUseUsageDescription,它仍然无法正常工作。 didChangeAuthorizationStatusdidFailWithError也永远不会被调用。 编辑:我能够得到它要求用户允许位置服务,但即使你点击允许它永远不会显示的位置。

目前iOS8 beta5中存在一个错误,它总是会为您的应用程序停用地理位置服务(至less在我的应用程序中)。

进入settings > Privacy > Location services > Your app > Always

但是我不知道为什么,但是即使您将其设置为“ Always此设置也会自动停用,因此请耐心等待,并经常返回设置以再次configuration您的应用位置。

尝试在头文件中声明CLLocationManager *locationManager然后它工作。 除非我已经声明它是一个全局variables,它不会要求许可和更新位置。

。H

 #import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> @interface ViewController : UIViewController <CLLocationManagerDelegate> { CLLocationManager *locationManager; } 

.M

 - (void)viewDidLoad { [super viewDidLoad]; locationManager = [[CLLocationManager alloc] init]; [locationManager setDelegate:(id)self]; [locationManager requestWhenInUseAuthorization]; [locationManager startMonitoringSignificantLocationChanges]; [locationManager startUpdatingLocation]; } 

委托方法

 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { NSLog(@"Getting Location"); } -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"%@", error.localizedDescription); } 

info.plis

在这里输入图像说明

到.plist文件中添加 在这里输入图像说明

 locationManager = [[CLLocationManager alloc] init]; [locationManager setDelegate:self]; [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters]; if(IS_OS_8_OR_LATER) { if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [locationManager requestWhenInUseAuthorization]; } } [locationManager startUpdatingLocation]; - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { currentLocation = [locations lastObject]; } 

所有步骤看起来都是完整的,但只有在检查:didChangeAuthorizationStatus后才能运行:startUpdatingLocation

此时,请确保authorizationStatus不是kCLAuthorizationStatusNotDeterminedkCLAuthorizationStatusDenied

如果是,请检查info.plist中的string条目。

如果您觉得所有这些都是正确的,请确保您从设备上卸载应用程序。 执行一个干净的版本,然后在设备上运行应用程序。

我有一个类似的问题,我的应用程序迁移到iOS 8.作为基于示例代码的原始代码,由苹果提供https://developer.apple.com/library/ios/samplecode/GeocoderDemo/Introduction/Intro.html我检查是否它已被更新,它已经&#x3002; 主要区别在于这个function与iOS 8相关的位已被评论。 这对我有效。

 - (void)startUpdatingCurrentLocation { // if location services are restricted do nothing if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted ) { return; } // if locationManager does not currently exist, create it if (self.locationManager == nil) { _locationManager = [[CLLocationManager alloc] init]; [self.locationManager setDelegate:self]; self.locationManager.distanceFilter = 10.0f; // we don't need to be any more accurate than 10m } // 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 ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [_locationManager requestWhenInUseAuthorization]; } [_locationManager startUpdatingLocation]; [self showCurrentLocationSpinner:YES]; } 

尝试

  1. 背景模式在能力

  2. #import <CoreLocation/CoreLocation.h>

  3. <CLLocationManagerDelegate>

  4. locationManager = [[CLLocationManager alloc] init]; [locationManager setDelegate:(id)self]; [locationManager requestWhenInUseAuthorization];

    这在应用程序的开始

  5. NSLocationWhenInUseUsageDescription中的plist

  6. 尝试CMD + SHIFT + K

  7. 尝试closuresiPhone,然后打开 – 他有一些iOS 8的现金 – 所以5次 – 应用程序不要求核心位置的权限 – 这帮助我

可能的原因是,为什么授权对话框没有与OP代码一起显示,地理位置pipe理员不得不随时待命。

使用属性或静态variables将locationManager保存在内存中。

也许是因为WhenInUseAuthorization是不够的“ 重大位置更改 ”更新。

当我使用AlwaysAuthorization ,它只是工作。

我无法在文档中find它。 但是你可以检查http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/ 。 在“授权types”标题下,它指出重要的位置更新需要AlwaysAuthorization。