【问题标题】:About the behaviour of ibeacons when app is in background or killed from background?关于应用程序在后台或从后台杀死时 ibeacons 的行为?
【发布时间】:2015-10-16 09:01:52
【问题描述】:

我使用的是 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];
    }    
  }
}

【问题讨论】:

  • 监控:进入/退出区域范围触发的动作;在前台、后台工作,甚至在应用程序被杀死时也能正常工作。测距:基于接近信标触发的动作;仅在前台工作。试试这个教程raywenderlich.com/66584/ios7-ibeacons-tutorial

标签: ios iphone ios8 ios9


【解决方案1】:

我希望这会给你答案。

如果您让发生重大变化的位置服务继续运行,而您的 iOS 应用随后被暂停或终止,则该服务会在新的位置数据到达时自动唤醒您的应用。 在唤醒时,应用程序进入后台,您有一小段时间(大约 10 秒)手动重新启动位置服务并处理位置数据。 (您必须在后台手动重新启动位置服务,然后才能交付任何挂起的位置更新)。

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

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

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

所以记住两件事

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多