OGeek|极客世界-中国程序员成长平台

标题: ios - didEnterRegion 仅在设备唤醒时触发 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 21:58
标题: ios - didEnterRegion 仅在设备唤醒时触发

我在我的应用中使用由 iBeacon 触发的本地通知。只要 iPhone 处于事件状态,它在前台和后台都可以正常工作,但在大约 15 分钟不活动或重新启动后不会触发 didEnterRegion

然后它只会在使用主页按钮或 sleep 按钮唤醒 iPhone 时再次触发,但我希望 didEnterRegion 在 iPhone 在口袋中放置 15 分钟或更长时间时“触发”也进入该地区。

这可能吗?如果是,怎么做?

后台模式 > 位置更新已禁用

一些代码:

.h

@property (strong, nonatomic) CLBeaconRegion *beaconRegion;
@property (strong, nonatomic) CLLocationManager *locationManager;

.m

- (void)start {
    self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier"com.bla.bla"];

    self.beaconRegion.notifyOnEntry = YES;
    self.beaconRegion.notifyOnExit = YES;
    self.beaconRegion.notifyEntryStateOnDisplay = YES; 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
    [self.locationManager startUpdatingLocation];

    [self.locationManager startMonitoringForRegion:self.beaconRegion];
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
    [self.locationManager requestStateForRegion:self.beaconRegion];
}

- (void)locationManagerCLLocationManager *)manager didEnterRegionCLRegion *)region {
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}

-(void)locationManagerCLLocationManager *)manager didExitRegionCLRegion *)region {
    [self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];
}

- (void)locationManagerCLLocationManager *)manager monitoringDidFailForRegionCLRegion *)region withErrorNSError *)error
{
    NSLog(@"%@", [error localizedDescription]);
} 

-(void)locationManagerCLLocationManager *)manager didDetermineStateCLRegionState)state forRegionCLRegion *)region {
    if (state == CLRegionStateInside) {
        [manager startRangingBeaconsInRegion:self.beaconRegion];
    } else {
        [manager stopRangingBeaconsInRegion:self.beaconRegion];
    }
}



Best Answer-推荐答案


我不确定这里发生了什么,但问题中描述的体验与我在多个设备上看到的测试不一致。发布设置它的代码可能有助于找出一些答案。

在几个应用程序中,我已经能够获得后台 didEnterRegion 回调,即使在没有按肩部按钮或主页按钮的情况下闲置超过 15 分钟后也是如此。为此,我不必设置任何背景模式。 (如果您将应用程序提交到商店并设置了不必要的后台模式位置更新,Apple 实际上会拒绝您的应用程序。)

iOS 7.1 中存在一个错误,它会在启动后的某个时间点停止 iBeacon 检测,所以也许这就是这种情况下发生的情况。详情为 here .不幸的是,测试这个假设需要您唤醒屏幕以关闭和打开蓝牙以清除条件,这将唤醒您的屏幕并让您退出区域。也许您可以尝试设置 beaconregion.notifyEntryStateOnDisplay=NO,重新创建此条件,然后尝试循环蓝牙以查看是否收到通知。您还可以使用现成的信标扫描应用程序,例如 Locate for iBeacon查看您的设备在进入此状态后是否能够针对 iBeacons 进行范围,如果您无法检测到 iBeacons,则仅循环到蓝牙。

关于ios - didEnterRegion 仅在设备唤醒时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23390832/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4