【问题标题】:iBeacon didEnterRegion not firing when in background and lockediBeacon didEnterRegion 在后台并锁定时未触发
【发布时间】:2014-06-03 07:50:09
【问题描述】:

我有一个 iBeacon 配置为:

NSUUID *proximityUUID = [[NSUUID alloc] initWithUUIDString:kUUID];
self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:proximityUUID identifier:kIdentifier];
self.beaconRegion.notifyEntryStateOnDisplay = NO;    
self.beaconRegion.notifyOnEntry = YES;
self.beaconRegion.notifyOnExit = YES;

当我的应用关闭并且设备被锁定时,didEnterRegion 永远不会被触发:

locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
    [self sendLocalNotificationForBeaconRegionHello:(CLBeaconRegion *)region];
}

- (void)sendLocalNotificationForBeaconRegionHello:(CLBeaconRegion *)region
{
    UILocalNotification *notification = [UILocalNotification new];

    notification.alertBody = [NSString stringWithFormat:@"Welcome - %@", region.identifier];
    notification.alertAction = NSLocalizedString(@"View", nil);
    notification.soundName = UILocalNotificationDefaultSoundName;
    notification.fireDate = nil;
    notification.hasAction = false;

    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}

即使应用程序已关闭且手机已锁定,也会调用 didExitRegion。我只有在解锁手机时进入区域时才会收到通知。

任何想法可能是什么问题?

谢谢

【问题讨论】:

  • 你在开始序列的某个地方使用了钥匙串吗?
  • 钥匙串是什么意思?
  • keychain = 苹果的安全密码存储
  • 不,我不在我的应用中使用钥匙串。

标签: ibeacon


【解决方案1】:

我怀疑该方法正在被调用,但由于某种原因通知被吞下(尽管代码看起来不错)。尝试添加如下方法来帮助弄清楚发生了什么:

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
    if(state == CLRegionStateInside) {
        NSLog(@"locationManager didDetermineState INSIDE for %@", region.identifier);
    }
    else if(state == CLRegionStateOutside) {
       NSLog(@"locationManager didDetermineState OUTSIDE for %@", region.identifier);
    }
    else {
        NSLog(@"locationManager didDetermineState OTHER for %@", region.identifier);
    }
}

didExitRegion 中查看您的代码以进行比较也很有用,并了解有关您如何测试进入/退出条件(包括等待时间)的详细信息。

【讨论】:

  • 我对 didExitRegion 使用相同的函数调用,但更改了消息。 didExitRegion 在离开 iBeacon 区域后大约 15 到 20 秒被触发。我等了 10 分钟才收到关于 didEnterRegion 的通知,但没有运气。解锁手机时,通知会在大约 1 到 3 秒后显示。
  • 查看我的更新答案。在添加如图所示的方法并重新运行测试后,我希望看到 NSLog 输出。
  • 做了一些测试......但得到了更奇怪的结果。当连接到笔记本电脑并调试应用程序时,didEnterRegion 确实会被触发。一旦我运行与笔记本电脑断开连接的应用程序,didEnterRegion 就永远不会被解雇。与笔记本电脑连接和断开连接时,didExitRegion 工作正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-30
  • 1970-01-01
相关资源
最近更新 更多