ios - Kontakt 信标和 iOS : didStartMonitoringForRegion not working as expected

标签 ios objective-c ibeacon kontakt.io

我开始在 iOS 上使用 Kontakt.io 信标,但即使我已按照 https://developer.kontakt.io/ios-sdk/quickstart/detecting-beacons/ 上的说明进行操作,以及 https://developer.kontakt.io/ios-sdk/quickstart/installation/ 中描述的第一步,看来我只能让它工作一次。

这是我的 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

玩断点,似乎 didChangeLocationAuthorizationStatus 总是被启动(我每次都能在屏幕上看到“YEAH!!!”消息)但是 didStartMonitoringForRegion 没有启动,除非我卸载并重新安装(杀死应用程序不起作用) 。顺便说一句,第一次安装时,范围信标的效果很好。正如您所看到的,我尝试过仅测距信标而不检查 onEnterRegion,但它不起作用。

编辑:将 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;

并且这次按预期工作。但我对这种行为有点困惑。该应用程序会保持该区域的监控,即使它被强制关闭?提前致谢!

最佳答案

据我所知,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. - source

关于ios - Kontakt 信标和 iOS : didStartMonitoringForRegion not working as expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43685432/

相关文章:

ios - 代码中的 UITabBarController 不显示导航栏

objective-c - 调用dispatch_sync()时发生死锁,即使它在另一个队列上

ios - iBeacon:如何将自定义数据放入 advdata?

ios - 如何引用 MMDrawerController 左/右导航 Controller 中的实例方法?

ios - 如何水平滚动collectionview?

ios - 如何在快速 Storyboard中将 TabBarController 放入另一个 TabBarController 中?

python - 什么更接近python None : nil or NULL?

java - Android Studio 通知管理器错误

ios - didEnterRegion 和 startRangingForBeacons 未被调用

ios - Firebase 和谷歌标签管理器 : <FIRAnalytics/WARNING> Attempted to log nil event