Receive signal from beacon while app is in the background

送分小仙女□ 提交于 2020-01-13 15:01:33

问题


I have a iOS app that interact with a beacon. Also have a function to detect the signal from a beacon and if the signal is out of range I want to store something in the database (in this case core data)

    func updateDistance(distance: CLProximity) {
    UIView.animateWithDuration(1.0) { [unowned self] in
        switch distance {
        case .Unknown:
            self.view.backgroundColor = UIColor.grayColor()
            self.distanceReading.text = "No Beacon Signal Detected"
            updateDatabaseWithLocation()

        }
    }
}

So the app works fine as long as the app is running in foreground. My understanding is I can also have the app in the background when user not actively using the app and still get signal from beacon. If so, how can I accomplish this task?
I added the following to info.plist and still didn't work.
Also is it possible to update the database as well?


回答1:


You can only range beacons (which is what gives you access to the distance approximations) when the app is running. As soon you leave the app (press the home button, navigate to another app, etc.), iOS will suspend the app, and ranging will stop working. Continuous usage of Bluetooth radio in the background would drain the smartphone's battery quickly.

You can, however, have your app subscribe to be woken up into the background when the smartphone enters and exits range of a beacon (or a group of beacons). This is called region monitoring, and it's the same mechanism that geofencing uses.

Now, "enter" and "exit" events in and on themselves won't give you access to distance approximations. However, since iOS will wake your app into the background for a few seconds to handle these events, ranging will actually resume for the duration (assuming you haven't stopped it before the app got suspended), before iOS puts the app back to sleep again.

You can even extend the "few seconds" into up to a few minutes with a background execution task.

All of the above doesn't require the use of background modes—only the "always" authorization to use Location Services.

You can't ordinarily keep the app running in the background indefinitely with beacons. Background support is heavily regulated by Apple, and is only allowed, e.g., for navigation apps, or music apps. People do on occasion try using the "location" background mode to keep the app alive in the background (and thus capable of ranging beacons), and some even reported being able to get it past the review process, but that seems to be more of an exception than a rule.

Should you decide to give it a try anyway, you need to:

  • enable the "location" background mode,
  • set allowsBackgroundLocationUpdates to true on your CLLocationManager instance,
  • start regular location updates: startUpdatingLocation.

This should keep the app running in the background even if you leave it.



来源:https://stackoverflow.com/questions/33984778/receive-signal-from-beacon-while-app-is-in-the-background

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!