【问题标题】:iBeacon - didEnterRegion not being called when app starts in regioniBeacon - 应用程序在区域中启动时未调用 didEnterRegion
【发布时间】:2014-07-30 18:33:47
【问题描述】:

我制作了一个应用程序,它注册信标区域并开始使用 CLLocationManager 监控这些区域

CLLocationManager *manager = [[CLLocationManager alloc] init];
manager.delegate = self;

CLBeaconRegion *region = [[CLBeaconRegion alloc] initWithProximityUUID:estimoteUUID major:12445 identifier:@"id"];
region.notifyEntryStateOnDisplay = YES;
region.notifyOnEntry = YES;
[manager startMonitoringForRegion:region];

当我从信标走得足够远并回到范围内时,这很有效。但是,如果我已经在信标区域的范围内启动应用程序,我也希望委托方法 didEnterRegion 触发,而不仅仅是当我回到边界时。有没有简单的方法来实现这一目标?或者让 CLLocationManager 认为我们已经离开信标范围的方法?

另一个帖子说设置region.notifyEntryStateOnDisplay = YES; 并按下保持按钮可以做到这一点 - 但我还没有得到这个工作(iOS 7.1,iPhone 5S)。

【问题讨论】:

    标签: ios cllocationmanager ibeacon estimote


    【解决方案1】:

    根据用户当前位置计算米中的值,信标的起点和终点应该在信标完整无线电内,然后强制调用didEnterRegion或执行您想做的任何操作。

    【讨论】:

    • 这个答案与 OP 询问的有关 iBeacon 区域监控的问题无关。即使不是iBeacon,也应该使用区域监控,而不是手动计算距离。
    【解决方案2】:

    尝试在startMonitoringForeRegion:之后插入这一行:[self.locationManager requestStateForRegion:region];。

    【讨论】:

    • 所以@Tys 这很奇怪,因为您的代码似乎很好。我曾与 Estimote Beacons 合作过,它们运行良好。你为什么不使用他们的SDK?
    • 我在其他部分使用了他们的 SDK - 我只是想我会展示这个问题的本机代码,所以它不是 Estimote SDK 特有的。不要误会我的意思,信标工作正常。当我已经在区域内启动应用程序时,我只想获得一个 didEnterRegion 回调:)
    • 我写的解决方案是在 iBeacons 教程中指出的,它符合您的描述。我不知道出了什么问题......而且我猜 Estimote 的人不会帮助你,因为他们提供了自己的 SDK。
    【解决方案3】:

    来自苹果的文档:

    地理区域的监控在之后立即开始 注册授权应用程序。但是,不要期望收到 立即发生事件,因为只有越界才会产生事件。 特别是,如果用户的位置已经在该区域内 在注册时,位置管理器不会自动 生成一个事件。相反,您的应用必须等待用户通过 事件生成前的区域边界并将其发送到 代表。 检查用户是否已经在边界内 一个区域,使用 requestStateForRegion: 方法 CLLocationManager 类。

    所以我最终这样做了:

    #import "ViewController.h"
    
    @interface ViewController ()
    @property (nonatomic, strong) NSDictionary *regionDictionary;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        // setup regions in case you have multiple regions
        self.regionDictionary = @{@"com.test" : @"2FAE2A83-1634-443B-8A0C-56704F81A181"};
    
        // setup location manager
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
    
        if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [self.locationManager requestAlwaysAuthorization];
        }
    
        [self.locationManager startUpdatingLocation];
    
        //start monitoring for all regions
        for (NSString *key in self.regionDictionary.allKeys) {
            CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:self.regionDictionary[key]] identifier:key];
            [self.locationManager startMonitoringForRegion:beaconRegion];
        }
    }
    
    - (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion *)region {
        if (region.identifier.length != 0) {
            CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:self.regionDictionary[region.identifier]] identifier:region.identifier];
            [self.locationManager startRangingBeaconsInRegion:beaconRegion];
        }
    }
    
    - (void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion *)region {
        if (region.identifier.length != 0) {
            CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:self.regionDictionary[region.identifier]] identifier:region.identifier];
            [self.locationManager stopRangingBeaconsInRegion:beaconRegion];
        }
    }
    
    - (void)locationManager:(CLLocationManager*)manager didRangeBeacons:(NSArray*)beacons inRegion:(CLBeaconRegion*)region {
        // Beacon found!
        CLBeacon *foundBeacon = [beacons firstObject];
        NSLog(@"UUID:%@; major:%@; minor:%@;", foundBeacon.proximityUUID.UUIDString, foundBeacon.major, foundBeacon.minor);
     }
    
    - (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {
        if ([region isKindOfClass:[CLBeaconRegion class]] && state == CLRegionStateInside) {
            [self locationManager:manager didEnterRegion:region];
        }
    }
    
    - (void)locationManager:(CLLocationManager *) manager didStartMonitoringForRegion:(CLRegion *) region {
        [manager requestStateForRegion:region];
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多