区域监测和背景

时间:2016-10-28 05:15:32

标签: ios objective-c background cllocationmanager region-monitoring

我毫不怀疑并想知道是否有必要再次将所有地区重新分配给位置管理员,如果它收到appEnterInBackGround的发布通知?

这是一些代码段。

- (IBAction)startAction:(id)sender
 {
  for (Geofencing *gObjects in plotingArrays) {
        CLCircularRegion *getRegion = [self dictToRegion:gObjects];
        [monitorLocationManager startMonitoringForRegion:getRegion];
    }
  }

因此,当应用程序进入后台时,我确实喜欢这样:

  # pragma mark - BackGround Notification
   -(void)applicationEnterBackground
    {
      monitorLocationManager = [selectRouteController sharedLocationMonitor];
      monitorLocationManager.delegate = self;
      for (Geofencing *gObjects in plotingArrays) {
        CLCircularRegion *getRegion = [self dictToRegion:gObjects];
        [monitorLocationManager startMonitoringForRegion:getRegion];
    }
  }

当应用程序进入后台时,是否有必要再次将区域重新分配给位置管理器?或者,在startAction:操作

上将区域分配给位置管理员后,它会自动监控

UPDATE1:

+ (CLLocationManager *)sharedLocationMonitor {
static CLLocationManager *locationMonitor;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

        locationMonitor = [[CLLocationManager alloc] init];
        locationMonitor.desiredAccuracy =  
         kCLLocationAccuracyBestForNavigation;

        locationMonitor.activityType = 
        CLActivityTypeAutomotiveNavigation;
        [locationMonitor requestAlwaysAuthorization];

        if(IS_OS_9_OR_LATER){
            locationMonitor.allowsBackgroundLocationUpdates = YES;
        }

        if(SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(@"8.4")){
            locationMonitor.pausesLocationUpdatesAutomatically = NO;
        }
    });
   return locationMonitor;
   }

PLIST:

App plist configuration

1 个答案:

答案 0 :(得分:1)

不,当您的应用在后台进入时,您无需重新启动区域监控。如果已配置区域,它将自动监视区域。

您需要在info.plist中配置以下内容:

<key>NSLocationAlwaysUsageDescription</key>
<string>I want to get your location Information in background</string>

<key>UIBackgroundModes</key>
<array>
    <string>location</string>
</array>

您还需要将AllowBackgroundLocationUpdates设置为yes。

 [monitorLocationManager setAllowsBackgroundLocationUpdates:YES];