我找到了这个用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");
}
答案 0 :(得分:2)
只有当用户越过区域边界时才会触发didEnterRegion和didExitRegion回调。在iBeacon的情况下,这意味着从“内部”移动到“外部”,反之亦然。
当您启动应用程序并开始监控您的信标区域时,您可以请求信标区域的当前状态,以确定您的用户是在室内还是室外。
实现didDetermineState回调:
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
在您开始监控您的区域后,任何时候超过区域边界时都会触发此回调(因此请注意不要在此处和在didEnter / ExitRegion内部复制逻辑),并且响应对requestStateForRegion的调用:
希望这有助于...如果您需要更多 - &gt; https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/index.html#//apple_ref/occ/intfm/CLLocationManagerDelegate/locationManager:didDetermineState:forRegion: