didEnterRegion 和 startRangingForBeacons 未被调用
didEnterRegion and startRangingForBeacons not being called
我一直无法弄清楚为什么 startRangingBeaconsInRegion 从未被调用过。我确定调用了 startMonitoringForRegion,并且我尝试将 mRegionsArray 输出为字符串并且它起作用了。但是没有调用 didEnterRegion。我试着来回走动,试图从我的信标获得信号(即进入该区域),但没有成功。我无法理解可能出了什么问题,在这里经历了很多问题,其中 none 反映了我的问题。
我有一个 Beacons table 视图,每个单元格都应该包含每个信标的信息(主要,次要)。除了,这些单元格没有被填充,因为测距没有发生。 :( 我什至试图改变它,让它只检测到一个信标。我知道问题不在我创建的信标 class 中,因为 loadTestData() 函数有效...
如果有人能提供帮助,将不胜感激。
BeaconTableViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface BeaconTableViewController : UITableViewController <CLLocationManagerDelegate>
@property (strong, nonatomic) CLBeaconRegion *beaconRegion;
@property (strong, nonatomic) CLLocationManager *locationManager;
@end
BeaconTableViewController.m
#import "BeaconTableViewController.h"
#import "Beacon.h"
#import "BeaconTableViewCell.h"
@interface BeaconTableViewController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *beaconsTableView;
@property (strong, nonatomic) NSMutableArray *beacons;
@end
@implementation BeaconTableViewController
- (void)loadTestData {
self.beacons = [[NSMutableArray alloc] init];
Beacon *beacon1 = [[Beacon alloc] init];
beacon1.major = [[NSNumber alloc] initWithInt:21311];
beacon1.minor = [[NSNumber alloc] initWithInt:21331];
[self.beacons addObject:beacon1];
Beacon *beacon2 = [[Beacon alloc] init];
beacon2.major = [[NSNumber alloc] initWithInt:10011];
beacon2.minor = [[NSNumber alloc] initWithInt:10012];
[self.beacons addObject:beacon2];
Beacon *beacon3 = [[Beacon alloc] init];
beacon3.major = [[NSNumber alloc] initWithInt:65535];
beacon3.minor = [[NSNumber alloc] initWithInt:30136];
[self.beacons addObject:beacon3];
[self.beaconsTableView beginUpdates];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:self.beacons.count-1 inSection:0];
[self.beaconsTableView insertRowsAtIndexPaths:@[newIndexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
[self.beaconsTableView endUpdates];
}
- (void)initRegion {
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"E2C56DB5-DFFB-48D2-B060-D0F5A71096E0"];
self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"AB Region"];
[self.locationManager startMonitoringForRegion:self.beaconRegion];
}
- (void) locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
UIAlertView *alertMonitoring = [[UIAlertView alloc] initWithTitle:@"User Notification"
message:@"Started monitoring for region."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertMonitoring show];
NSSet *mRegions = [self.locationManager monitoredRegions];
NSArray *mRegionsArray = [mRegions allObjects];
NSString *str = [mRegionsArray componentsJoinedByString:@","];
UIAlertView *alertRegion = [[UIAlertView alloc] initWithTitle:@"User Notification"
message:str
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertRegion show];
}
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
UIAlertView *alertRanging = [[UIAlertView alloc] initWithTitle:@"User Notification"
message:@"Started ranging."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertRanging show];
CLBeacon *foundBeacon = [beacons firstObject];
Beacon *beacon;
beacon.major = foundBeacon.major;
beacon.minor = foundBeacon.minor;
UIAlertView *alertBeaconFound = [[UIAlertView alloc] initWithTitle:@"User Notification"
message:[[[@"Major: " stringByAppendingString:[NSString stringWithFormat:@"%@", beacon.major]] stringByAppendingString:@", Minor: "] stringByAppendingString:[NSString stringWithFormat:@"%@", beacon.minor]]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertBeaconFound show];
[self.beacons addObject:beacon];
[self.beaconsTableView beginUpdates];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:self.beacons.count-1 inSection:1];
[self.beaconsTableView insertRowsAtIndexPaths:@[newIndexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
[self.beaconsTableView endUpdates];
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"User Notification"
message:@"Did enter region."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
[self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];
}
- (void)viewDidLoad {
[super viewDidLoad];
// [self loadTestData];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self initRegion];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.beacons count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BeaconTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BeaconCell" forIndexPath:indexPath];
Beacon *beacon = [self.beacons objectAtIndex:indexPath.row];
cell.beacon = beacon;
return cell;
}
在 iOS8 下,Apple 添加了一些使用位置管理器的新要求(iBeacons 是位置管理器功能)
您必须将密钥 NSLocationAlwaysUsageDescription
and/or NSLocationWhenInUseUsageDescription
添加到您的 info.plist 文件中,然后在尝试开始监控信标之前您必须检查授权状态如果是 kCLAuthorizationStatusNotDetermined,则必须重新调用 requestAlwaysAuthorization
或 requestWhenInUseAuthorization
代码可能如下所示:
CLAuthorizationStatus status =[CLLocationManager authorizationStatus];
if (status ==kCLAuthorizationStatusDenied)
{
NSLog(@"Location manager denied");
}
theLocManager = [[CLLocationManager alloc] init];
theLocManager.delegate = self;
if (status == kCLAuthorizationStatusNotDetermined
&& [theLocManager respondsToSelector: @selector(requestAlwaysAuthorization)])
[theLocManager requestAlwaysAuthorization];
(您必须添加检查以确保位置管理响应 requestAlwaysAuthorization
或 requestWhenInUseAuthorization
方法,因为它们仅在 iOS >= 8 中可用。)
我不喜欢此 OS 更改的一点是,如果您不发出请求调用,则开始监控信标的调用会自动失败。
我一直无法弄清楚为什么 startRangingBeaconsInRegion 从未被调用过。我确定调用了 startMonitoringForRegion,并且我尝试将 mRegionsArray 输出为字符串并且它起作用了。但是没有调用 didEnterRegion。我试着来回走动,试图从我的信标获得信号(即进入该区域),但没有成功。我无法理解可能出了什么问题,在这里经历了很多问题,其中 none 反映了我的问题。
我有一个 Beacons table 视图,每个单元格都应该包含每个信标的信息(主要,次要)。除了,这些单元格没有被填充,因为测距没有发生。 :( 我什至试图改变它,让它只检测到一个信标。我知道问题不在我创建的信标 class 中,因为 loadTestData() 函数有效...
如果有人能提供帮助,将不胜感激。
BeaconTableViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface BeaconTableViewController : UITableViewController <CLLocationManagerDelegate>
@property (strong, nonatomic) CLBeaconRegion *beaconRegion;
@property (strong, nonatomic) CLLocationManager *locationManager;
@end
BeaconTableViewController.m
#import "BeaconTableViewController.h"
#import "Beacon.h"
#import "BeaconTableViewCell.h"
@interface BeaconTableViewController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *beaconsTableView;
@property (strong, nonatomic) NSMutableArray *beacons;
@end
@implementation BeaconTableViewController
- (void)loadTestData {
self.beacons = [[NSMutableArray alloc] init];
Beacon *beacon1 = [[Beacon alloc] init];
beacon1.major = [[NSNumber alloc] initWithInt:21311];
beacon1.minor = [[NSNumber alloc] initWithInt:21331];
[self.beacons addObject:beacon1];
Beacon *beacon2 = [[Beacon alloc] init];
beacon2.major = [[NSNumber alloc] initWithInt:10011];
beacon2.minor = [[NSNumber alloc] initWithInt:10012];
[self.beacons addObject:beacon2];
Beacon *beacon3 = [[Beacon alloc] init];
beacon3.major = [[NSNumber alloc] initWithInt:65535];
beacon3.minor = [[NSNumber alloc] initWithInt:30136];
[self.beacons addObject:beacon3];
[self.beaconsTableView beginUpdates];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:self.beacons.count-1 inSection:0];
[self.beaconsTableView insertRowsAtIndexPaths:@[newIndexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
[self.beaconsTableView endUpdates];
}
- (void)initRegion {
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"E2C56DB5-DFFB-48D2-B060-D0F5A71096E0"];
self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"AB Region"];
[self.locationManager startMonitoringForRegion:self.beaconRegion];
}
- (void) locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
UIAlertView *alertMonitoring = [[UIAlertView alloc] initWithTitle:@"User Notification"
message:@"Started monitoring for region."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertMonitoring show];
NSSet *mRegions = [self.locationManager monitoredRegions];
NSArray *mRegionsArray = [mRegions allObjects];
NSString *str = [mRegionsArray componentsJoinedByString:@","];
UIAlertView *alertRegion = [[UIAlertView alloc] initWithTitle:@"User Notification"
message:str
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertRegion show];
}
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
UIAlertView *alertRanging = [[UIAlertView alloc] initWithTitle:@"User Notification"
message:@"Started ranging."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertRanging show];
CLBeacon *foundBeacon = [beacons firstObject];
Beacon *beacon;
beacon.major = foundBeacon.major;
beacon.minor = foundBeacon.minor;
UIAlertView *alertBeaconFound = [[UIAlertView alloc] initWithTitle:@"User Notification"
message:[[[@"Major: " stringByAppendingString:[NSString stringWithFormat:@"%@", beacon.major]] stringByAppendingString:@", Minor: "] stringByAppendingString:[NSString stringWithFormat:@"%@", beacon.minor]]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertBeaconFound show];
[self.beacons addObject:beacon];
[self.beaconsTableView beginUpdates];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:self.beacons.count-1 inSection:1];
[self.beaconsTableView insertRowsAtIndexPaths:@[newIndexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
[self.beaconsTableView endUpdates];
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"User Notification"
message:@"Did enter region."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
[self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];
}
- (void)viewDidLoad {
[super viewDidLoad];
// [self loadTestData];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self initRegion];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.beacons count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BeaconTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BeaconCell" forIndexPath:indexPath];
Beacon *beacon = [self.beacons objectAtIndex:indexPath.row];
cell.beacon = beacon;
return cell;
}
在 iOS8 下,Apple 添加了一些使用位置管理器的新要求(iBeacons 是位置管理器功能)
您必须将密钥 NSLocationAlwaysUsageDescription
and/or NSLocationWhenInUseUsageDescription
添加到您的 info.plist 文件中,然后在尝试开始监控信标之前您必须检查授权状态如果是 kCLAuthorizationStatusNotDetermined,则必须重新调用 requestAlwaysAuthorization
或 requestWhenInUseAuthorization
代码可能如下所示:
CLAuthorizationStatus status =[CLLocationManager authorizationStatus];
if (status ==kCLAuthorizationStatusDenied)
{
NSLog(@"Location manager denied");
}
theLocManager = [[CLLocationManager alloc] init];
theLocManager.delegate = self;
if (status == kCLAuthorizationStatusNotDetermined
&& [theLocManager respondsToSelector: @selector(requestAlwaysAuthorization)])
[theLocManager requestAlwaysAuthorization];
(您必须添加检查以确保位置管理响应 requestAlwaysAuthorization
或 requestWhenInUseAuthorization
方法,因为它们仅在 iOS >= 8 中可用。)
我不喜欢此 OS 更改的一点是,如果您不发出请求调用,则开始监控信标的调用会自动失败。