Ranging Beacons only works when app running?

后端 未结 5 707
说谎
说谎 2021-01-29 23:05

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

5条回答
  •  无人及你
    2021-01-29 23:44

    Code for iOS 9 to range beacons in the background, by using Location Updates:

    1. Open Project Settings -> Capabilities -> Background Modes -> Toggle Location Updates and Uses Bluetooth LE accessories to ON.

    2. 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
      
    3. 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))
      
    4. 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-

提交回复
热议问题