ios - 如果 iBeacon 已经在范围内,则找不到 iBeacon

标签 ios ibeacon

我找到了这个关于使用 iBeacon 开发应用程序的好教程:

http://www.appcoda.com/ios7-programming-ibeacons-tutorial/

但是对于这个实现,正如作者所说,如果你在接收器已经在信标范围内时启动它,它就不会发射。如果你想找到一个 ibeacon,你需要远离它的区域,然后走回范围内。

我如何修改此代码以在我用应用程序吃午饭时找到它在范围内的信标?

我用的是Xcode6、iPad air、IOS 8

这是教程中的简化代码:

在 ViewController.h 中

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController<CLLocationManagerDelegate>

@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion;
@property (strong, nonatomic) CLLocationManager *locationManager;

@end

在 ViewController.m 中

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;

NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"ACFD065E-C3C0-11E3-9BBE-1A514932AC01"];

self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
                                               identifier:@"com.appcoda.testregion"];

[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
}




- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion *)region
 {
    NSLog(@"Finding beacons.");
    [self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
}




-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion *)region
{
    NSLog(@"None found.");
    [self.locationManager stopRangingBeaconsInRegion:self.myBeaconRegion];
}




-(void)locationManager:(CLLocationManager*)manager
   didRangeBeacons:(NSArray*)beacons
          inRegion:(CLBeaconRegion*)region
{
NSLog(@"Beacon found");
}

最佳答案

didEnterRegion 和 didExitRegion 回调仅在用户跨越区域边界时触发。对于 iBeacon,这意味着从“内部”移动到“外部”,反之亦然。

当您启动应用程序并开始监控您的信标区域时,您可以请求信标区域的当前状态以确定您的用户是在内部还是外部。

实现 didDetermineState 回调:

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region

此回调在您开始监控您的区域后触发,只要跨越区域边界(所以请注意您不要在此处和 didEnter/ExitRegion 内部重复逻辑),并响应对 requestStateForRegion 的调用:

希望这对您有所帮助...如果您需要更多 -> https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/index.html#//apple_ref/occ/intfm/CLLocationManagerDelegate/locationManager:didDetermineState:forRegion :

关于ios - 如果 iBeacon 已经在范围内,则找不到 iBeacon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27002548/

相关文章:

ios - 警告 : "This app will not work with future versions of iOS"

ios - 如何在 iOS 中模拟按下主页按钮?

ios - 在 UITableView 中,cellForRowAtIndexPath 忽略前几行

ios - CLLocationManager 和 iBeacons : is requestState(for: region) necessary?

ios - 多个 CLLocationManager 有超过 20 个监控区域

ios - 建筑物中如何获取Near IBeacon的经纬度?

ios - 如何打开 native GoogleMaps App 和 Waze App

ios - 用户切换选项卡栏项目后导航项目未更新

ios - iOS在后台监视/调整信标会消耗大量电池

android - 找到最近的信标