我正在构建一个应用程序来检查用户是否在我客户的商店附近,如果是,它会向他发送通知。
我也希望应用在后台检查它。
我已将此行添加到我的 info.plist
文件中:
这是我的代码:
AppDelegate.m:
-(BOOL)applicationUIApplication *)application willFinishLaunchingWithOptionsNSDictionary *)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)locationManagerCLLocationManager *)manager didUpdateLocationsNSArray<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 详细说明了当应用处于后台时获取更新所需的步骤。基本上你必须在你的应用委托(delegate)方法中截取一个键。这个SO question有示例代码。
但是在您跳到代码之前,请按照建议查看讨论,因为它会让您更清楚地了解如何掌握这种行为。与往常一样,您也总是希望监控讨论中提到的失败委托(delegate)方法。
Apple 已发布此非常重要的说明,说明位置更新发送到设备的频率限制。为了节省电池,他们尝试限制更新次数,直到设备移动 500 米。您的测试应考虑这些限制。
资源
有关更多补充信息,请注意以下对这些 SO 问题的回答:
关于ios - CLLocationManager 委托(delegate)方法未在后台调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35171872/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |