关于应用程序在后台或从后台杀死时ibeacons的行为?

时间:2015-10-16 09:01:52

标签: ios iphone ios8 ios9

我正在使用Ibeacon模板示例,因为我正在使用本地通知。当应用不在后台“didExitRegion”“didEnterRegion”“ didRangeBeacons“方法随机调用。我不清楚这些方法在后台和背景中是如何工作的,有没有人可以帮我解决这个问题。谢谢。

这是我正在使用的示例代码:

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
      [manager stopRangingBeaconsInRegion:(CLBeaconRegion*)region];
      [self.locationManager stopUpdatingLocation];
      NSLog(@"You exited the region.");
      [self sendLocalNotificationWithMessage:@"You exited the region."];
}

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    [manager startRangingBeaconsInRegion:(CLBeaconRegion*)region];
    [self.locationManager startUpdatingLocation];
    NSLog(@"You entered the region.");
    [self sendLocalNotificationWithMessage:@"You entered the region."];
}

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
    NSString *message = @"i am in 3 meters.";
   IMViewController *viewController = (IMViewController*)self.window.rootViewController;
   viewController.beacons = beacons;
   [viewController.tableView reloadData];

   if(beacons.count > 0) {
      CLBeacon *nearestBeacon = beacons.firstObject;
      if(nearestBeacon.proximity == self.lastProximity ||
       nearestBeacon.proximity == CLProximityUnknown)
      {
         return;
      }
    self.lastProximity = nearestBeacon.proximity;
    NSLog(@"lastProximity: %ld", (long)self.lastProximity);
    NSInteger str=(int)nearestBeacon.accuracy;
    //NSString *distance=[NSString stringWithFormat:@"Distance: %d",(int)nearestBeacon.accuracy];
    if (str ==3)
    {
        [self sendLocalNotificationWithMessage:message];
    }    
  }
}

1 个答案:

答案 0 :(得分:0)

我希望这会给你答案。

如果您使重要更改位置服务保持运行且您的iOS应用随后被暂停或终止,则当新位置数据到达时,该服务会自动唤醒您的应用。 在唤醒时,应用程序将被置于后台,您将获得少量时间(大约10秒)来手动重新启动位置服务并处理位置数据。 (在传递任何挂起的位置更新之前,您必须在后台手动重新启动位置服务)。

因为你的应用程序是在后台,它必须做最少的工作并避免任何任务(大约10秒),或者它可以使用UIApplication类的 beginBackgroundTaskWithName:expirationHandler:方法请求更多的后台执行时间

注意:当用户全局或为您的应用禁用后台应用刷新设置时,重要更改位置服务不会启动您的应用。此外,当后台应用刷新关闭时即使它位于前台,app也不会收到重大变化或区域监控事件。

因此,当用户进入/退出区域时,也会在相同的情况下启动应用程序。

所以记住两件事

  1. 启用后台应用刷新设置。
  2. 如果您的代码需要超过5~10秒,请使用beginBackgroundTaskWithName:expirationHandler。