【问题标题】:didRangeBeacon not workingdidRangeBeacon 不工作
【发布时间】:2015-03-27 17:41:32
【问题描述】:

我不知道我是否错过了我的信标设置。我尝试了一切,但代码对我不起作用。我的信标是 kontakt.io。我尝试了 sdk 和示例代码,但仍然无法正常工作。 这是我的代码:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import  <CoreLocation/CoreLocation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import "KontaktSDK.h"

    @interface ViewController : UIViewController <KTKLocationManagerDelegate,KTKActionManagerDelegate,KTKBluetoothManagerDelegate>

    @property (strong,nonatomic) KTKLocationManager * ktkLocationManger;
    @property (strong,nonatomic) CLLocationManager * locationManager;
    @property (strong,nonatomic) KTKRegion * ktkRegion;
    @property (strong,nonatomic) KTKActionManager * actionManager;
    @property (strong,nonatomic) KTKBluetoothManager * bluetoothManager;
    @property (strong,nonatomic) KTKBeaconManager * beaconManager;
    @end

还有我的 viewcontroller.h

#import "ViewController.h"


@interface ViewController ()


@property (weak, nonatomic) IBOutlet UILabel *beaconLabel;

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];

    self.ktkLocationManger = [[KTKLocationManager alloc] init];
    self.ktkLocationManger.delegate = self;
    // Do any additional setup after loading the view, typically from a nib.

    if ([KTKLocationManager canMonitorBeacons])
    {
        KTKRegion *region =[[KTKRegion alloc] init];
        region.uuid = @"A77A1B68-49A7-4DBF-914C-760D07FBB87B"; // kontakt.io proximity UUID


        [self.ktkLocationManger setRegions:@[region]];
        [self.ktkLocationManger startMonitoringBeacons];


        self.bluetoothManager = [[KTKBluetoothManager alloc] init];
        self.bluetoothManager.delegate = self;

        self.beaconManager = [[KTKBeaconManager alloc] init];
        self.beaconManager.delegate = self;

        [self.beaconManager startFindingDevices];
    }


}

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

-(void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];

    [self.ktkLocationManger startMonitoringBeacons];

}

- (void)locationManager:(KTKLocationManager *)locationManager didChangeState:(KTKLocationManagerState)state withError:(NSError *)error
{
    if (state == KTKLocationManagerStateFailed)
    {
        NSLog(@"Something went wrong with your Location Services settings. Check OS settings.");
    }
}

- (void)locationManager:(KTKLocationManager *)locationManager didEnterRegion:(KTKRegion *)region
{
    NSLog(@"Enter region %@", region.uuid);
    self.beaconLabel.text=@"enter";
}

- (void)locationManager:(KTKLocationManager *)locationManager didExitRegion:(KTKRegion *)region
{
    NSLog(@"Exit region %@", region.uuid);
    self.beaconLabel.text=@"exit";
}

- (void)locationManager:(KTKLocationManager *)locationManager didRangeBeacons:(NSArray *)beacons
{
    NSLog(@"Ranged beacons count: %lu", [beacons count]);
    self.beaconLabel.text=@"Range";

}

我尝试了一切只是为了使用 didRangeBeacons: 但多次失败。 有什么我需要设置的,或者我只是错过了代码。希望得到您的建议或帮助。提前谢谢

【问题讨论】:

    标签: ios objective-c ibeacon


    【解决方案1】:

    您还记得将NSLocationAlwaysUsageDescription 添加到项目的Info.plist 文件中吗?这在 iOS 8 中是必需的。

    在 Project Navigator(左侧栏)中,找到 Info.plist 文件(它位于 Supporting Files 目录中),右键单击它并选择 Open As > Source Code。然后,将其添加到 &lt;/dict&gt; 行之前:

    <key>NSLocationAlwaysUsageDescription</key>
    <string>This app uses your location in order to blah blah...</string>
    

    描述应该解释为什么您的应用程序需要访问用户的位置数据。您给出的解释越好,用户允许的机会就越大,所以请明智地选择! (:

    【讨论】:

    • 我已经这样做了@heypiotr。我想问一下。 didRangeBeacons:方法需要来自信标的 wifi 连接?
    • 我现在明白了。当我从 iPhone 设置中关闭飞行模式时。它可以工作,但是当我打开时,什么也没发生。每个信标都需要关闭手机的飞行模式设置吗?
    • 信标使用低能耗蓝牙 4.0,而不是 WiFi。飞行模式通常会禁用蓝牙,从而阻止手机发现信标。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 2018-12-22
    • 2016-04-19
    • 1970-01-01
    相关资源
    最近更新 更多