Note that there are some explanatory texts on larger screens.

plurals
  1. POControlling view from iBeacon proximity
    primarykey
    data
    text
    <p>I'm playing with Corelocation and iBeacons. I have notifications triggered when entering and exiting a region, and I can range my beacons etc. That's all good.</p> <p>However I'm getting into a mess. I want to load a second view when near to a specific beacon, then close that view when we move away from that beacon, rinse and repeat for a second beacon.</p> <p>I'm struggling with:</p> <ul> <li><p>how to stop triggering the change of view because the ranging doesn't stop. If I manually stop ranging or use a bool to test if I'm already in the second view this is ok but seems messy.</p></li> <li><p>how to close the view if I move away from the beacon. To do this I guess I can't stop the ranging, otherwise I'd not know if I moved away.</p></li> </ul> <p>My code is below.</p> <pre><code> - (void)viewDidLoad { [super viewDidLoad]; } - (void)viewDidAppear:(BOOL)animated { // Setup Beacon Manager self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; NSUUID *uuid = [[NSUUID alloc]initWithUUIDString:@"B9407F30-F5F8-466E-AFF9-25556B57FE6D"]; self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"Beacon Region"]; [self.locationManager startMonitoringForRegion:self.beaconRegion]; [self.locationManager requestStateForRegion:self.beaconRegion]; self.beaconStatLabel.text = @"StartLocationServices"; //check to see if this is the first time we've run the app if ([[NSUserDefaults standardUserDefaults] floatForKey:@"tBHasRun"] == 0) { [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"tBHasRun"]; //set the time run to 1 [self performSegueWithIdentifier:@"firstRunSegue" sender:self]; self.beaconStatLabel.text = @"FIRST RUN"; //set the label text } else { self.beaconStatLabel.text = @"REPEAT RUN"; //set the label text } } //Looking for and dealing with regions - (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region { //looking for a region means looking for a beacon or set of beacons that share a UUID [self.locationManager requestStateForRegion:self.beaconRegion]; } - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { //if we found a region we start ranging (looking for beaocns) [self.locationManager startRangingBeaconsInRegion:self.beaconRegion]; self.regionState.text = @"Region Entered"; //we'll also test sending a notification UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.alertBody = @"Welcome! Go upstairs, bring beer."; notification.soundName = UILocalNotificationDefaultSoundName; [[UIApplication sharedApplication] presentLocalNotificationNow:notification]; } - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region { //we have left the region so we'll stop ranging [self.locationManager stopRangingBeaconsInRegion:self.beaconRegion]; self.regionState.text = @"Region Exited"; //we'll also test sending a notification UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.alertBody = @"Thankyou for coming. For information on our next MeetUp check our MeetUp page."; notification.soundName = UILocalNotificationDefaultSoundName; [[UIApplication sharedApplication] presentLocalNotificationNow:notification]; } //dealing with individual beacons - (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region { //once beacons are ranged we enter this method //we'll grab and log the signal strength of the beacons for (int i = 0; i &lt; [beacons count]; i++) { CLBeacon *singleBeacon = [[CLBeacon alloc]init]; singleBeacon = [beacons objectAtIndex:i]; } //we get the latest beacon in the array - the closest beacon (strongest signal) CLBeacon *beacon = [[CLBeacon alloc] init]; beacon = [beacons lastObject]; //update the info labels self.uuidLabel.text = beacon.proximityUUID.UUIDString; self.majorLabel.text = [NSString stringWithFormat:@"%@", beacon.major]; self.minorLabel.text = [NSString stringWithFormat:@"%@", beacon.minor]; //we store some information about that beacon NSNumber *beaconMajor = beacon.major; //it's major (group) number NSNumber *beaconMinor = beacon.minor; //it's minor (individual) number //we then call the manageBeacon method and pass through the minor, major, and proximity values [self manageBeaconWithMinor:beaconMinor AndMajor:beaconMajor AtRange:beacon.proximity]; } - (void)manageBeaconWithMinor:(NSNumber *)minorNumber AndMajor:(NSNumber *)majorNumber AtRange:(CLProximity)proximity { //in this method we work out what do do based upon the beacon we are connected to and the range //for this test we'll look for the mint beacon and call a view if (([minorNumber floatValue] == 59204) &amp;&amp; ([majorNumber floatValue] == 33995) &amp;&amp; (proximity == CLProximityNear)) { //we are going to open up content [[tBGlobalStore sharedInstance]setInContentTrue]; NSLog([[tBGlobalStore sharedInstance] getInContent] ? @"Yes" : @"No"); //the beacon numbers match the beacon we are expecting so we'll call the next screen [self performSegueWithIdentifier:@"mainToContent" sender:self]; } } </code></pre> <p>Some additional information from the console, you can see it's trying to call the view more than once.</p> <p>2013-11-22 15:24:56.487 testingBeacons[670:60b] --- 2013-11-22 15:24:56.489 testingBeacons[670:60b] ----- 2013-11-22 15:24:56.490 testingBeacons[670:60b] Beacon at: 2013-11-22 15:24:56.490 testingBeacons[670:60b] 0 2013-11-22 15:24:56.491 testingBeacons[670:60b] has RSSI of: 2013-11-22 15:24:56.492 testingBeacons[670:60b] -75 2013-11-22 15:24:56.492 testingBeacons[670:60b] ----- 2013-11-22 15:24:56.493 testingBeacons[670:60b] --- 2013-11-22 15:24:56.495 testingBeacons[670:60b] Warning: Attempt to present on whose view is not in the window hierarchy! 2013-11-22 15:24:56.497 testingBeacons[670:60b] --- 2013-11-22 15:24:56.498 testingBeacons[670:60b] ----- 2013-11-22 15:24:56.498 testingBeacons[670:60b] Beacon at: 2013-11-22 15:24:56.499 testingBeacons[670:60b] 0 2013-11-22 15:24:56.499 testingBeacons[670:60b] has RSSI of: 2013-11-22 15:24:56.500 testingBeacons[670:60b] -75 2013-11-22 15:24:56.500 testingBeacons[670:60b] ----- 2013-11-22 15:24:56.501 testingBeacons[670:60b] --- 2013-11-22 15:24:57.487 testingBeacons[670:60b] --- 2013-11-22 15:24:57.489 testingBeacons[670:60b] ----- 2013-11-22 15:24:57.489 testingBeacons[670:60b] Beacon at: 2013-11-22 15:24:57.490 testingBeacons[670:60b] 0 2013-11-22 15:24:57.490 testingBeacons[670:60b] has RSSI of: 2013-11-22 15:24:57.491 testingBeacons[670:60b] -75 2013-11-22 15:24:57.491 testingBeacons[670:60b] ----- 2013-11-22 15:24:57.492 testingBeacons[670:60b] --- 2013-11-22 15:24:57.493 testingBeacons[670:60b] Warning: Attempt to present on whose view is not in the window hierarchy! 2013-11-22 15:24:57.495 testingBeacons[670:60b] --- 2013-11-22 15:24:57.495 testingBeacons[670:60b] ----- 2013-11-22 15:24:57.496 testingBeacons[670:60b] Beacon at: 2013-11-22 15:24:57.496 testingBeacons[670:60b] 0 2013-11-22 15:24:57.497 testingBeacons[670:60b] has RSSI of: 2013-11-22 15:24:57.497 testingBeacons[670:60b] -75 2013-11-22 15:24:57.498 testingBeacons[670:60b] ----- 2013-11-22 15:24:57.499 testingBeacons[670:60b] --- 2013-11-22 15:24:57.500 testingBeacons[670:60b] Warning: Attempt to present on whose view is not in the window hierarchy! 2013-11-22 15:24:58.488 testingBeacons[670:60b] --- 2013-11-22 15:24:58.489 testingBeacons[670:60b] ----- 2013-11-22 15:24:58.490 testingBeacons[670:60b] Beacon at: 2013-11-22 15:24:58.490 testingBeacons[670:60b] 0 2013-11-22 15:24:58.491 testingBeacons[670:60b] has RSSI of: 2013-11-22 15:24:58.491 testingBeacons[670:60b] -76 2013-11-22 15:24:58.492 testingBeacons[670:60b] ----- 2013-11-22 15:24:58.492 testingBeacons[670:60b] --- 2013-11-22 15:24:58.493 testingBeacons[670:60b] Warning: Attempt to present on whose view is not in the window hierarchy! 2013-11-22 15:24:58.494 testingBeacons[670:60b] --- 2013-11-22 15:24:58.495 testingBeacons[670:60b] ----- 2013-11-22 15:24:58.496 testingBeacons[670:60b] Beacon at: 2013-11-22 15:24:58.496 testingBeacons[670:60b] 0 2013-11-22 15:24:58.497 testingBeacons[670:60b] has RSSI of: 2013-11-22 15:24:58.497 testingBeacons[670:60b] -76 2013-11-22 15:24:58.498 testingBeacons[670:60b] ----- 2013-11-22 15:24:58.499 testingBeacons[670:60b] --- 2013-11-22 15:24:58.500 testingBeacons[670:60b] Warning: Attempt to present on whose view is not in the window hierarchy! 2013-11-22 15:24:59.488 testingBeacons[670:60b] --- 2013-11-22 15:24:59.489 testingBeacons[670:60b] ----- 2013-11-22 15:24:59.489 testingBeacons[670:60b] Beacon at: 2013-11-22 15:24:59.490 testingBeacons[670:60b] 0 2013-11-22 15:24:59.490 testingBeacons[670:60b] has RSSI of: 2013-11-22 15:24:59.491 testingBeacons[670:60b] -75 2013-11-22 15:24:59.491 testingBeacons[670:60b] ----- 2013-11-22 15:24:59.492 testingBeacons[670:60b] --- 2013-11-22 15:24:59.493 testingBeacons[670:60b] Warning: Attempt to present on whose view is not in the window hierarchy! 2013-11-22 15:24:59.494 testingBeacons[670:60b] --- 2013-11-22 15:24:59.495 testingBeacons[670:60b] ----- 2013-11-22 15:24:59.495 testingBeacons[670:60b] Beacon at: 2013-11-22 15:24:59.496 testingBeacons[670:60b] 0 2013-11-22 15:24:59.496 testingBeacons[670:60b] has RSSI of: 2013-11-22 15:24:59.497 testingBeacons[670:60b] -75 2013-11-22 15:24:59.498 testingBeacons[670:60b] ----- 2013-11-22 15:24:59.498 testingBeacons[670:60b] --- 2013-11-22 15:24:59.500 testingBeacons[670:60b] Warning: Attempt to present on whose view is not in the window hierarchy! 2013-11-22 15:25:00.487 testingBeacons[670:60b] --- 2013-11-22 15:25:00.488 testingBeacons[670:60b] ----- 2013-11-22 15:25:00.489 testingBeacons[670:60b] Beacon at: 2013-11-22 15:25:00.489 testingBeacons[670:60b] 0 2013-11-22 15:25:00.490 testingBeacons[670:60b] has RSSI of: 2013-11-22 15:25:00.490 testingBeacons[670:60b] -75 2013-11-22 15:25:00.491 testingBeacons[670:60b] ----- 2013-11-22 15:25:00.491 testingBeacons[670:60b] --- 2013-11-22 15:25:00.492 testingBeacons[670:60b] Warning: Attempt to present on whose view is not in the window hierarchy! 2013-11-22 15:25:00.493 testingBeacons[670:60b] --- 2013-11-22 15:25:00.494 testingBeacons[670:60b] ----- 2013-11-22 15:25:00.494 testingBeacons[670:60b] Beacon at: 2013-11-22 15:25:00.495 testingBeacons[670:60b] 0 2013-11-22 15:25:00.495 testingBeacons[670:60b] has RSSI of: 2013-11-22 15:25:00.496 testingBeacons[670:60b] -75 2013-11-22 15:25:00.496 testingBeacons[670:60b] ----- 2013-11-22 15:25:00.497 testingBeacons[670:60b] --- 2013-11-22 15:25:00.498 testingBeacons[670:60b] Warning: Attempt to present on whose view is not in the window hierarchy! 2013-11-22 15:25:01.488 testingBeacons[670:60b] --- 2013-11-22 15:25:01.489 testingBeacons[670:60b] ----- 2013-11-22 15:25:01.489 testingBeacons[670:60b] Beacon at: 2013-11-22 15:25:01.490 testingBeacons[670:60b] 0 2013-11-22 15:25:01.490 testingBeacons[670:60b] has RSSI of: 2013-11-22 15:25:01.491 testingBeacons[670:60b] -72 2013-11-22 15:25:01.492 testingBeacons[670:60b] ----- 2013-11-22 15:25:01.492 testingBeacons[670:60b] --- 2013-11-22 15:25:01.493 testingBeacons[670:60b] Warning: Attempt to present on whose view is not in the window hierarchy! 2013-11-22 15:25:01.494 testingBeacons[670:60b] --- 2013-11-22 15:25:01.495 testingBeacons[670:60b] ----- 2013-11-22 15:25:01.495 testingBeacons[670:60b] Beacon at: 2013-11-22 15:25:01.496 testingBeacons[670:60b] 0 2013-11-22 15:25:01.497 testingBeacons[670:60b] has RSSI of: 2013-11-22 15:25:01.497 testingBeacons[670:60b] -72 2013-11-22 15:25:01.498 testingBeacons[670:60b] ----- 2013-11-22 15:25:01.498 testingBeacons[670:60b] --- 2013-11-22 15:25:01.499 testingBeacons[670:60b] Warning: Attempt to present on whose view is not in the window hierarchy!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload