CLLocationManager 委托方法未在后台调用
CLLocationManager delegate methods not being called on background
我正在构建一个应用程序来检查用户是否在我客户的商店附近,如果在附近,它会向他发送通知。
我也希望应用程序在后台检查它。
我已将此行添加到我的 info.plist
文件中:
这是我的代码:
AppDelegate.m:
-(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self configureLocationManager];
[self.locationManager startUpdatingLocation];
return YES;
}
-(void)configureLocationManager
{
//Initializing locationManager
self.locationManager=[[CLLocationManager alloc] init];
//setting "locationManager"'s(CLLocationManager) delegate to "self"
self.locationManager.delegate=self.monitorLocationVC;
//Setting "locationManager"'s(CLLocationManager)'s distance filter to none
//self.locationManager.distanceFilter=kCLDistanceFilterNone;
//Setting "locationManager"'s(CLLocationManager)'s activityType to navigation
self.locationManager.activityType=CLActivityTypeAutomotiveNavigation;
//setting "locationManager"'s(CLLocationManager) desiredAccuracy to "best"
self.locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
//If OS version is 9 or above - setting "allowsBackgroundLocationUpdates" to YES
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
self.locationManager.allowsBackgroundLocationUpdates = YES;
}
}
MonitorLocationViewController.m:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
[self checkIfNearStore]; //Not being called in background
}
-(void)checkIfNearStore
{
for (Store *currentStore in self.allStores) {
if ([currentStore.circularRegion containsCoordinate:self.locationManager.location.coordinate]&¤tStore.alreadySendNotification==NO) {
NSLog(@"Entered: %@",[[self storeForRegion:currentStore.circularRegion] address]);
currentStore.alreadySendNotification=YES;
[self.storesAlreadySentNotifications addObject:currentStore];
}
}
for (Store *currentStore in self.storesAlreadySentNotifications) {
if (![currentStore.circularRegion containsCoordinate:self.locationManager.location.coordinate]) {
currentStore.alreadySendNotification=NO;
}
}
}
有人有想法吗?谢谢!
后台位置更新
我建议您重新仔细阅读与startMonitoringSignificantLocationChanges方法相关的讨论(您必须使用此方法来解决问题)。请注意,您可以通过调用 stopMonitoringSignificantLocationChanges 然后再次调用 start... 方法来强制进行新的更新。您还需要在 locationManager:didUpdateLocations 方法中监控时间戳。
Apple 详细说明了应用程序在后台运行时获取更新所需的步骤。基本上你必须在你的应用程序委托方法中拦截一个键。此 有示例代码。
但在跳转到代码之前,请按照建议查看讨论,因为它会让您更清楚地了解如何掌握此行为。一如既往,您也将始终希望监视讨论中提到的失败委托方法。
苹果Documentation
诺塔贝内
Apple 发布了关于位置更新发送到设备的频率限制的非常重要的说明。为了节省电池电量,他们尝试限制更新次数,直到设备移动 500 米。您的测试应考虑这些限制。
资源
有关更多补充信息,请注意以下对这些 SO 问题的回答:
不幸的是,由于 NDA,我无法 post 我用于地理围栏的代码,但这篇文章可以帮助您实现您想要的:https://corgitoergosum.net/2013/01/12/geo-fencing-in-timetraveler-v2-part-i-of-3/
我正在构建一个应用程序来检查用户是否在我客户的商店附近,如果在附近,它会向他发送通知。
我也希望应用程序在后台检查它。
我已将此行添加到我的 info.plist
文件中:
这是我的代码:
AppDelegate.m:
-(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self configureLocationManager];
[self.locationManager startUpdatingLocation];
return YES;
}
-(void)configureLocationManager
{
//Initializing locationManager
self.locationManager=[[CLLocationManager alloc] init];
//setting "locationManager"'s(CLLocationManager) delegate to "self"
self.locationManager.delegate=self.monitorLocationVC;
//Setting "locationManager"'s(CLLocationManager)'s distance filter to none
//self.locationManager.distanceFilter=kCLDistanceFilterNone;
//Setting "locationManager"'s(CLLocationManager)'s activityType to navigation
self.locationManager.activityType=CLActivityTypeAutomotiveNavigation;
//setting "locationManager"'s(CLLocationManager) desiredAccuracy to "best"
self.locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
//If OS version is 9 or above - setting "allowsBackgroundLocationUpdates" to YES
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
self.locationManager.allowsBackgroundLocationUpdates = YES;
}
}
MonitorLocationViewController.m:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
[self checkIfNearStore]; //Not being called in background
}
-(void)checkIfNearStore
{
for (Store *currentStore in self.allStores) {
if ([currentStore.circularRegion containsCoordinate:self.locationManager.location.coordinate]&¤tStore.alreadySendNotification==NO) {
NSLog(@"Entered: %@",[[self storeForRegion:currentStore.circularRegion] address]);
currentStore.alreadySendNotification=YES;
[self.storesAlreadySentNotifications addObject:currentStore];
}
}
for (Store *currentStore in self.storesAlreadySentNotifications) {
if (![currentStore.circularRegion containsCoordinate:self.locationManager.location.coordinate]) {
currentStore.alreadySendNotification=NO;
}
}
}
有人有想法吗?谢谢!
后台位置更新
我建议您重新仔细阅读与startMonitoringSignificantLocationChanges方法相关的讨论(您必须使用此方法来解决问题)。请注意,您可以通过调用 stopMonitoringSignificantLocationChanges 然后再次调用 start... 方法来强制进行新的更新。您还需要在 locationManager:didUpdateLocations 方法中监控时间戳。
Apple 详细说明了应用程序在后台运行时获取更新所需的步骤。基本上你必须在你的应用程序委托方法中拦截一个键。此
但在跳转到代码之前,请按照建议查看讨论,因为它会让您更清楚地了解如何掌握此行为。一如既往,您也将始终希望监视讨论中提到的失败委托方法。
苹果Documentation
诺塔贝内
Apple 发布了关于位置更新发送到设备的频率限制的非常重要的说明。为了节省电池电量,他们尝试限制更新次数,直到设备移动 500 米。您的测试应考虑这些限制。
资源
有关更多补充信息,请注意以下对这些 SO 问题的回答:
不幸的是,由于 NDA,我无法 post 我用于地理围栏的代码,但这篇文章可以帮助您实现您想要的:https://corgitoergosum.net/2013/01/12/geo-fencing-in-timetraveler-v2-part-i-of-3/