不在背景上调用CLLocationManager委托方法

我正在构build一个应用程序,检查用户是否在我的客户的商店附近,如果有,它会向他发送一个通知。

我希望应用程序在后台检查它。

我已经将这行添加到我的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]&&currentStore.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; } } } 

任何人有一个想法? 谢谢!

不幸的是,由于NDA,我不能发布我用于geofencing的代码,但是这篇文章可以帮助你实现你想要的: https : //corgitoergosum.net/2013/01/12/geo-fencing-in-timetraveler- V2-部分-I-的-3 /

背景位置更新

我build议您仔细阅读与startMonitoringSignificantLocationChanges方法有关的讨论(您必须使用此方法来解决问题)。 请注意,您可以通过调用stopMonitoringSignificantLocationChanges然后再次调用start …方法来强制更新。 您还需要监视locationManager中的时间戳:didUpdateLocations方法。

苹果详细介绍了当应用程序处于后台时需要获取更新的步骤。 基本上,你将不得不拦截你的应用程序委托方法中的一个键。 这个问题有示例代码。

但在跳转到代码之前,请按照build议回顾讨论,因为这会让您更清楚地了解如何掌握此行为。 与往常一样,您也总是要监视讨论中提到的失败委托方法。

Apple 文档

significantlocationchanges

Nota Bene

苹果已经发布了这个非常重要的注意事项来限制位置更新发送到设备的频率。 为了省电,他们尝试限制更新次数,直到设备移动500米。 你的testing应该考虑这些限制。

500米

资源

如需更多补充信息,请注意以下对这些SO问题的回答:

#1 #2