我开始在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(我每次都可以在屏幕上看到&#34; YEAH !!!&#34;消息)但是除非我卸载并重新安装,否则不会启动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;
这一次按预期工作。但我对这种行为有点困惑。该应用程序保持区域监控,即使它被强行关闭?提前谢谢!
答案 0 :(得分:1)
据我所知,Kontakt.io的SDK基于Apple的Core Location,因此以下内容也适用:
在iOS中,始终跟踪与您的应用相关联的区域, 包括应用程序未运行时。 - source