不在背景上调用CLLocationManager委派方法 [英] CLLocationManager delegate methods not being called on background
问题描述
我正在建立一个应用程式,检查使用者是否在我的客户的商店附近,如果有,则会传送通知给他。
我已将此行添加到我的 info.plist
文件:
这里是我的代码:
AppDelegate.m :
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ ;
[self.locationManager startUpdatingLocation];
return YES;
}
- (void)configureLocationManager
{
//初始化locationManager
self.locationManager = [[CLLocationManager alloc] init];
//设置locationManager的(CLLocationManager)委托为self
self.locationManager.delegate = self.monitorLocationVC;
//将locationManager的(CLLocationManager)的距离过滤器设置为none
//self.locationManager.distanceFilter=kCLDistanceFilterNone;
//将locationManager的(CLLocationManager)的activityType设置为导航
self.locationManager.activityType = CLActivityTypeAutomotiveNavigation;
//设置locationManager的(CLLocationManager)desiredAccuracy为最好
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
//如果操作系统版本是9或更高 - 设置allowBackgroundLocationUpdates为YES
if([[[UIDevice currentDevice] systemVersion] floatValue]> = 9){
self.locationManager。 allowedBackgroundLocationUpdates = YES;
}
}
MonitorLocationViewController.m
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray< CLLocation *> *)locations
{
[self checkIfNearStore]; //不在后台调用
}
- (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]地址]);
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,我无法发布用于地理围栏的代码,但本文可以帮助您实现您想要的: https ://corgitoergosum.net/2013/01/12/geo-fencing-in-timetraveler-v2-part-i-of-3/
I'm building an application that checks if the user is near a store of my client, if it does, it sends him a notification.
I want the app to check it in the background as well.
I've added this lines to my info.plist
file:
and here is my code:
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;
}
}
}
Anybody have an idea? Thanks!
Unfortunately, I can't post the code I use for geofencing due to NDA, but this article could help you achieve what you want: https://corgitoergosum.net/2013/01/12/geo-fencing-in-timetraveler-v2-part-i-of-3/
这篇关于不在背景上调用CLLocationManager委派方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!