简体   繁体   中英

How to stop beacon monitoring

I have developed an ios app which has two screens and the app scans for beacons in first screen(working properly). But when i navigate to second screen, it still scans for the beacon. My requirement is to stop the beacon scanning in second screen and it should restart scanning when it navigates back to first screen.

I am not using any vendor SDK's. I am using apples core location framework only.

I have used [self.locationManager stopUpdatingLocation] to stop and [self.locationManager startUpdatingLocation] to restart but both are not working. Please suggest how to achieve that.

Additional information: Device:-ipad, ios8.4

When you say "scanning for beacons" using CoreLocation, people usually are referring to the Beacon Ranging APIs. For this the way you start and stop are by making the calls:

[locationManager startRangingBeaconsInRegion: region]; // start

[locationManager stopRangingBeaconsInRegion: region]; // stop

If you want to do this in a ViewController when it appears and disappears, you can put your calls inside the viewWillAppear and viewWillDisappear callbacks. Like this:

- (void)viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:animated];
  [locationManager stopRangingBeaconsInRegion: region];
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  [locationManager startRangingBeaconsInRegion: region];
  }
}

Note that for the above code to work, you must have class variables set up for both region and locationManager .

You can stop monitoring for regions using :

Swift
func stopMonitoringForRegion(_ region: CLRegion!)
OBJECTIVE-C
- (void)stopMonitoringForRegion:(CLRegion *)region:

https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instm/CLLocationManager/stopMonitoringForRegion :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM