我一直无法弄清楚为什么从未调用startRangingBeaconsInRegion。我肯定知道调用了startMonitoringForRegion,并且我尝试将mRegionsArray输出为字符串并且它有效。但是,并没有调用didEnterRegion。我试着来回走来试图从我的信标中获取信号(即进入该地区),但没有运气。我无法绕过可能出错的地方,在这里经历了很多问题,但没有一个能反映我的问题。
我有一个Beacons表视图,每个单元应该包含每个信标上的信息(主要的,次要的)。除此之外,这些细胞没有被填充,因为测距没有发生。 :(我甚至试图改变它,所以它只检测到一个信标。我知道问题不在我创建的Beacon类中,因为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;
}
答案 0 :(得分:2)
在iOS 8下,Apple添加了一些使用位置管理器的新要求(而iBeacons是一个位置管理器功能)
您必须将密钥NSLocationAlwaysUsageDescription
和/或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&gt; = 8中可用。)
我不喜欢这个操作系统更改的事情是,如果你不进行请求调用,那么开始监视信标的调用将无声地失败。