简体   繁体   English

Kontakt信标和iOS:didStartMonitoringForRegion无法按预期工作

[英]Kontakt beacons and iOS: didStartMonitoringForRegion not working as expected

I'm starting to work with Kontakt.io beacons on iOS, but even when I've followed the instructions on https://developer.kontakt.io/ios-sdk/quickstart/detecting-beacons/ and the first steps described on https://developer.kontakt.io/ios-sdk/quickstart/installation/ , seems that I can only make it works once. 我开始在iOS上使用Kontakt.io信标,但是即使按照我在https://developer.kontakt.io/ios-sdk/quickstart/detecting-beacons/上的说明以及在https://developer.kontakt.io/ios-sdk/quickstart/installation/ ,看来我只能使其工作一次。

Here's my ViewController's code: 这是我的ViewController的代码:

#import "ViewController.h"
#import <KontaktSDK/KontaktSDK.h>

@interface ViewController () <KTKBeaconManagerDelegate>

@property KTKBeaconManager *beaconManager;
@property KTKBeaconRegion *region1;

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.beaconManager = [[KTKBeaconManager alloc] initWithDelegate:self];
    NSUUID *myProximityUUID = [[NSUUID alloc] initWithUUIDString:@"xxxxxxxxx...xxxx"];
    _region1 = [[KTKBeaconRegion alloc] initWithProximityUUID:myProximityUUID identifier:@"Beacon_1"];

    switch ([KTKBeaconManager locationAuthorizationStatus]) {
        case kCLAuthorizationStatusNotDetermined:
            [self.beaconManager requestLocationAlwaysAuthorization];
            break;

        case kCLAuthorizationStatusDenied:
        case kCLAuthorizationStatusRestricted:
            // No access to Location Services
            break;

        case kCLAuthorizationStatusAuthorizedWhenInUse:
            // For most iBeacon-based app this type of
            // permission is not adequate
            break;

        case kCLAuthorizationStatusAuthorizedAlways:
            // We will use this later
            if ([KTKBeaconManager isMonitoringAvailable]) {
                [self.beaconManager startMonitoringForRegion:_region1];
            }
            break;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)beaconManager:(KTKBeaconManager *)manager didChangeLocationAuthorizationStatus:(CLAuthorizationStatus)status {
    if (status == kCLAuthorizationStatusAuthorizedAlways) {
        _TheLabel.text = @"YEAH";
        if ([KTKBeaconManager isMonitoringAvailable]) {
            _TheLabel.text = @"YEAH!!!";
            [self.beaconManager startMonitoringForRegion:_region1];
        }
        // When status changes to kCLAuthorizationStatusAuthorizedAlways
        // e.g. after calling [self.beaconManager requestLocationAlwaysAuthorization]
        // we can start region monitoring from here
    }
}

//
- (void)beaconManager:(KTKBeaconManager *)manager didStartMonitoringForRegion:(__kindof KTKBeaconRegion *)region {
    // Do something when monitoring for a particular
    // region is successfully initiated
    _MonitoringStatus.text = @"Success";
    [manager startRangingBeaconsInRegion:region];
}

- (void)beaconManager:(KTKBeaconManager *)manager monitoringDidFailForRegion:(__kindof KTKBeaconRegion *)region withError:(NSError *)error {
    // Handle monitoring failing to start for your region
    _MonitoringStatus.text = @"FAIL!";
}

- (void)beaconManager:(KTKBeaconManager *)manager didEnterRegion:(__kindof KTKBeaconRegion *)region {
    // Decide what to do when a user enters a range of your region; usually used
    // for triggering a local notification and/or starting a beacon ranging
    [manager startRangingBeaconsInRegion:region];
    _OnRegionStatus.text = @"We're in!";
}

- (void)beaconManager:(KTKBeaconManager *)manager didExitRegion:(__kindof KTKBeaconRegion *)region {
    // Decide what to do when a user exits a range of your region; usually used
    // for triggering a local notification and stoping a beacon ranging
    [manager stopRangingBeaconsInRegion:region];
    _OnRegionStatus.text = @"We're out";
}

- (void)beaconManager:(KTKBeaconManager *)manager didRangeBeacons:(NSArray<CLBeacon *> *)beacons inRegion:(__kindof KTKBeaconRegion *)region {
    for(CLBeacon *beacon in beacons){
        _TheLabel.text = [NSString stringWithFormat:@"WOW! %ld", (long)[beacon proximity]];
    }
}

@end

Playing with breakpoints, seems that the didChangeLocationAuthorizationStatus is always being launched (I can see the "YEAH!!!" message on screen every time) but the didStartMonitoringForRegion is not launched unless I uninstall and reinstall (killing app is not working). 玩断点时,didChangeLocationAuthorizationStatus似乎总是在启动(每次我都可以在屏幕上看到“ YEAH !!!”消息),但是除非卸载并重新安装(杀死应用程序无法正常工作),否则didStartMonitoringForRegion不会启动。 It does range beacons fine that first time installed, by the way. 顺便说一句,它的范围信标可以在第一次安装时很好。 As you can see, I've tried just ranging beacons without checking the onEnterRegion, but it didn't work. 如您所见,我已经尝试过在不检查onEnterRegion的情况下仅对信标进行测距,但这没有用。

EDIT: Updated the viewDidLoad with: 编辑:更新了viewDidLoad与:

case kCLAuthorizationStatusAuthorizedAlways:
            // We will use this later
            if ([KTKBeaconManager isMonitoringAvailable]) {
                if([[self.beaconManager monitoredRegions] count] == 0) [self.beaconManager startMonitoringForRegion:_region1];
                else for(KTKBeaconRegion *reg in [self.beaconManager monitoredRegions]){
                    [self.beaconManager startRangingBeaconsInRegion:reg];
                }
            }
            break;

And this time is working as expected. 这次工作按预期进行。 But I'm a bit confused about this behaviour. 但是我对此行为有些困惑。 The app keeps the region monitorized, even when it was forcefully closed? 即使强制关闭该应用程序,该区域仍可对其进行监视? Thanks in advance! 提前致谢!

As far as I know, Kontakt.io's SDK is based on Apple's Core Location, so the following also applies: 据我所知,Kontakt.io的SDK是基于Apple的Core Location,因此以下内容也适用:

In iOS, regions associated with your app are tracked at all times, including when the app isn't running. 在iOS中,始终跟踪与您的应用相关联的区域,包括应用未运行时。 - source - 来源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM