複数のビーコンを使用する iPad 用のアプリを開発しており、その領域に入ったときにビューを表示する必要があります。次に、ビーコンの近くにいて、他のビーコンから遠く離れているときに、最初のビーコンを表示する必要があります。 2つ目を閉じて開きます。
私のコードはこれです:
-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
CLBeacon *beacon = [[CLBeacon alloc] init];
beacon = [beacons lastObject];
SectionViewController *sectionView = [[SectionViewController alloc] init];
Sections2ViewController *sectionView2 = [[Sections2ViewController alloc] init];
if ((beacon.proximity == CLProximityImmediate) && viewShown == NO && beacon.accuracy != -1.000000) {
if ((beacon.minor.integerValue != actualSection) && actualSection != 0) {
[self dismissViewControllerAnimated:NO completion:nil];
actualSection = 0;
NSLog(@"View Out");
}
if (beacon.minor.integerValue == 1) {
[self presentViewController:sectionView animated:NO completion:nil];
sectionView.section = 1;
actualSection = 1;
sectionView.lbl_name.text = self.lbl_name.text;
NSLog(@"View In: 1");
viewShown = YES;
}else if (beacon.minor.integerValue == 2){
[self presentViewController:sectionView2 animated:NO completion:nil];
sectionView.section = 2;
actualSection = 2;
NSLog(@"View In: 2");
viewShown = YES;
}
}else if ((beacon.proximity == CLProximityNear || beacon.proximity == CLProximityFar) && viewShown == YES && beacon.accuracy != -1.000000){
[self dismissViewControllerAnimated:NO completion:nil];
viewShown = NO;
}else{
NSLog(@"Unknown Distance");
}
NSLog(@"\nUUID: %@\nProximity:%ld\nMajor:%@\nMinor:%@\nAccuracy:%f\nRSSI:%ld\nSection: %ld", beacon.proximityUUID.UUIDString, beacon.proximity, beacon.major, beacon.minor, beacon.accuracy, (long)beacon.rssi, (long)self.section);
}
sectionView と SectionView2 は、表示したいビューです。
viewShown を使用して、ビューが表示されているか、mainView にいるかを確認します。
actualSection は、私がどこにいるかを知ることです。
問題は、ビーコンを 1 つだけ使用すると完璧に動作しますが、2 つ使用するとアプリが両方を検出せず、正しいビューを表示するのが難しいことです。
私は今それを試していますが、アプリは1つのビーコンとimのみを検出し、それらの両方で同じUUIDを使用し、マイナー値のみを変更しています。
現在、iPad Mini Retina で ios 7.0.4 を使用していますが、まもなく iOS 7.1 にアップデートする予定です。問題はありますか?
解決済み:
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}
- (void)initRegion {
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"00000000-0000-0000-0000-000000000002"];
self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"com.devfright.myRegion"];
[self.locationManager startMonitoringForRegion:self.beaconRegion];
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSLog(@"Beacon Found");
[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
NSLog(@"Left Region");
[self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];
}
-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
CLBeacon *beacon = [[CLBeacon alloc] init];
beacon = [beacons lastObject];
//Search for the closest and show the view
for (int i=0; i<beacons.count; i++) {
beacon = beacons[i];
if (beacon.proximity == CLProximityUnknown) {
NSLog(@"Unknown Proximity");
} else if ((beacon.proximity == CLProximityImmediate) && viewShown == NO && beacon.accuracy != -1.000000) {
if ([beacon.minor.stringValue isEqual: @"1"]) {
actualSection = 1;
[self immediateDetection];
}else if ([beacon.minor.stringValue isEqual: @"2"]){
actualSection = 2;
[self immediateDetection];
}else if ([beacon.minor.stringValue isEqual: @"3"]){
actualSection = 3;
[self immediateDetection];
}else if ([beacon.minor.stringValue isEqual: @"4"]){
actualSection = 4;
[self immediateDetection];
}
} else if ((beacon.proximity == CLProximityNear || beacon.proximity == CLProximityFar) && viewShown == YES && beacon.accuracy != -1.000000) {
if (beacon.minor.integerValue == actualSection) {
[self dismissViewControllerAnimated:NO completion:nil];
actualSection = 0;
viewShown = NO;
}
} else if (beacon.proximity == CLProximityFar) {
NSLog(@"Far");
}
}
}
- (void)immediateDetection
{
viewShown = YES;
if (self.presentedViewController)
{
return;
}
if (actualSection == 1) {
SectionViewController *sectionView = [[SectionViewController alloc] init];
sectionView.section = 1;
[self presentViewController:sectionView animated:NO completion:nil];
sectionView.lbl_name.text = self.lbl_name.text;
}else if (actualSection == 2){
SectionViewController *sectionView = [[SectionViewController alloc] init];
sectionView.section = 2;
[self presentViewController:sectionView animated:NO completion:nil];
sectionView.lbl_name.text = self.lbl_name.text;
}
}
これで正しく、近いものを検出します。ありがとう