在后台运行时从信标接收信号 [英] Receive signal from beacon while app is in the background

查看:119
本文介绍了在后台运行时从信标接收信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与信标交互的iOS应用. 还具有检测信标信号的功能,如果信号超出范围,我想在数据库中存储某些内容(在这种情况下为核心数据)

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()

        }
    }
}

因此,只要该应用程序在前台运行,它就可以正常运行. 我的理解是,当用户不积极使用应用程序而仍然从信标接收信号时,我也可以在后台运行该应用程序. 如果是这样,我如何完成此任务?
我在info.plist中添加了以下内容,但仍然无法使用.
也可以更新数据库吗?

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?

推荐答案

当应用运行时,您只能范围信标(通过它可以访问距离近似值).离开应用程序后(按主页"按钮,导航到另一个应用程序等),iOS将暂停该应用程序,并且测距将停止工作.在后台连续使用蓝牙无线电会迅速耗尽智能手机的电池.

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.

现在,本身发生的进入"和退出"事件将使您无法获得距离近似值.但是,由于iOS会将您的应用在后台唤醒几秒钟以处理这些事件,因此 ranging 实际上会在持续时间(假设您在应用暂停之前未停止它)恢复运行iOS使该应用重新进入睡眠状态.

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.

您甚至可以通过后台执行任务.

以上所有内容均不需要使用后台模式,仅需要使用位置服务的始终"授权即可.

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

通常无法使用信标使应用程序无限期地在后台运行. Apple严格规定了后台支持,例如,仅在导航应用程序或音乐应用程序中允许使用后台支持.人们有时会尝试使用位置"后台模式来使应用程序在后台保持活动状态(并因此能够定位信标),甚至有人报告说能够通过审核过程来获取该应用程序,但这似乎更多例外而不是规则.

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.

这篇关于在后台运行时从信标接收信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆