I am having difficulties getting this to work for when the app is not running. I have locationManager:didRangeBeacons:inRegion:
implemented and it is called when th
Code for iOS 9 to range beacons in the background, by using Location Updates:
Open Project Settings -> Capabilities -> Background Modes -> Toggle Location Updates
and Uses Bluetooth LE accessories
to ON
.
Create a CLLocationManager
, request Always
monitoring authorization (don't forget to add the Application does not run in background
to NO
and NSLocationAlwaysUsageDescription
in the app's info.plist
) and set the following properties:
locationManager!.delegate = self
locationManager!.pausesLocationUpdatesAutomatically = false
locationManager!.allowsBackgroundLocationUpdates = true
Start ranging for beacons and monitoring region:
locationManager!.startMonitoringForRegion(yourBeaconRegion)
locationManager!.startRangingBeaconsInRegion(yourBeaconRegion)
locationManager!.startUpdatingLocation()
// Optionally for notifications
UIApplication.sharedApplication().registerUserNotificationSettings(
UIUserNotificationSettings(forTypes: .Alert, categories: nil))
Implement the CLLocationManagerDelegate
and in your didEnterRegion
send both startRangingBeaconsInRegion()
and startUpdatingLocation()
messages (optionally send the notification as well) and set the stopRangingBeaconsInRegion()
and stopUpdatingLocation()
in didExitRegion
Be aware that this solution works but it is not recommended by Apple due to battery consumption and customer privacy!
More here: https://community.estimote.com/hc/en-us/articles/203914068-Is-it-possible-to-use-beacon-ranging-in-the-background-