【发布时间】:2015-10-29 19:02:37
【问题描述】:
我在很多应用中都使用过定位服务,但这个新应用在 iOS 8.0 及更高版本上存在问题。我没有收到有关应用程序首次加载提示以允许定位服务的通知。但是,在我的 iOS 7.1 设备上,我得到了提示。
这是我的 appDelegate 在 didFinishLaunchingWithOptions 中的内容:
self.locationManager = [CLLocationManager new];
[self.locationManager setDelegate:self];
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
[self initializeRegionMonitoring];
而initializeRegionMonitoring方法是:
-(void) initializeRegionMonitoring {
NSLog(@"initialize region monitoring");
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// radius of region being monitored
CLLocationDistance radius = 25; // 20 metre sensitivity
CLLocationCoordinate2D coordinate;
coordinate.latitude = 25.886099;
coordinate.longitude = -80.165124;
self.someRegion = [[CLCircularRegion alloc] initWithCenter:coordinate radius:radius identifier:@"Qualex"];
self.someRegion.notifyOnEntry = YES;
self.someRegion.notifyOnExit = YES;
[self.locationManager startMonitoringForRegion:self.someRegion];
// notify changes when the device has moved x meters
self.locationManager.distanceFilter = 20; // or set to 20 meters
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.activityType = CLActivityTypeAutomotiveNavigation;
[self.locationManager startUpdatingLocation];
[self.locationManager startMonitoringSignificantLocationChanges];
}
我还在 info.plist 中设置了 NSLocationAlwaysUsageDescription,所以肯定有什么我忘记了,对吧?感谢您的帮助!
编辑:
我同时也在注册远程通知,这以前从来不是问题,但我认为这可能是一些有用的额外信息。
此外,当应用程序终止时,重新运行通知以允许位置服务弹出,但会立即消失。只是在屏幕上闪烁。我不知道为什么它会在不单击警报上的选项之一的情况下关闭。
【问题讨论】:
-
您是否打算在
initializeRegionMonitoring方法中替换您的locationManager实例?第二个实例永远不会调用requestAlwaysAuthorization,这可能是您的问题的根源。 -
您真的打算像这样使用两个不同的位置管理器吗?
-
@thelaws 这应该没关系。位置授权在当前应用程序中跨所有 CLLocationManager 实例是全局的。用户授予始终在线位置访问权限后,所有位置管理员都将拥有该访问权限
-
@n00neimp0rtant 感谢您的澄清
-
在设置窗口之前还是之后提示用户?
标签: ios objective-c cllocationmanager