【发布时间】:2014-02-15 23:00:04
【问题描述】:
我正在编写两个简单的应用程序。一个是信标应用程序,您可以通过轻触按钮来启动或停止其信号。另一种是接收器应用程序,它在检测到信标信号时编辑标签的文本。
我尝试使用 didDetermineStateForRegion、didExitRegion 和 didEnterRegion 方法来检测应用程序何时运行。这些可以很好地确定接收器何时移入和移出信标附近,但确定我已关闭信标上的蓝牙大约需要 30 秒。我也尝试将我的 CLLocationManager 的 pausesLocationUpdatesAutomatically 字段设置为 NO,但同样的事情。理想情况下,它会立即在我的标签上加上“不”;我该怎么做?
MyView.h
@interface MyView : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion;
@property (strong, nonatomic) CLLocationManager *locationManager;
@end
MyView.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Initialize location manager and set ourselves as the delegate
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.pausesLocationUpdatesAutomatically=NO;
// Create a NSUUID with the same UUID as the broadcasting beacon
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"ID"];
// Setup a new region with that UUID and same identifier as the broadcasting beacon
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:@"identifier"];
// Tell location manager to start monitoring for the beacon region
[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
}
- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion*)region
{
[self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
self.statusLabel.text = @"Yes";
}
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {
if (state == CLRegionStateInside) {
self.statusLabel.text = @"Yes";
} else {
self.statusLabel.text = @"No";
}
}
-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion*)region
{
[self.locationManager stopRangingBeaconsInRegion:self.myBeaconRegion];
self.statusLabel.text = @"No";
}
【问题讨论】: