【发布时间】:2014-11-18 19:36:46
【问题描述】:
我找到了这个使用 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");
}
【问题讨论】: