On didEnter
/ didExit
, the app is being woken up into the background for 10 seconds if it’s not running. Thinking in terms of the UIApplicationState enum, the app needs to be in the UIApplicationStateActive
state in order for startLocationUpdates
to work. When an app gets woken up into the background via didEnter
/ didExit
, it’s in the UIApplicationStateBackground
state instead.
UPDATE: just found a way to make starting and stopping location updates in the background work. If you startMonitoringSignificantLocationChanges
in the foreground (you can’t start this one in the background), then startLocationUpdates
will work in the background. (CC @Morgan_Zysman)
let beaconManager = ESTBeaconManager()
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
beaconManager.delegate = self
beaconManager.requestAlwaysAuthorization()
beaconManager.startMonitoringForRegion(beaconRegion)
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
locationManager.allowsBackgroundLocationUpdates = true
locationManager.startMonitoringSignificantLocationChanges()
}
func beaconManager(manager: AnyObject!, didEnterRegion region: CLBeaconRegion!) {
print("didEnter")
locationManager.startUpdatingLocation()
beaconManager.startRangingBeaconsInRegion(beaconRegion)
}
func beaconManager(manager: AnyObject!, didExitRegion region: CLBeaconRegion!) {
print("didExit")
locationManager.stopUpdatingLocation()
beaconManager.stopRangingBeaconsInRegion(beaconRegion)
}
func beaconManager(manager: AnyObject!, didRangeBeacons beacons: [AnyObject]!, inRegion region: CLBeaconRegion!) {
print("didRangeBeacons: \(beacons)")
}