英文:
Detect iBeacon signals in iOS during terminated state
问题
有三种应用程序状态
- 前台
- 后台
- 终止
iBeacons 在前台和后台正常工作并向用户发送通知。
在终止状态下,如何检测用户进入 iBeacon 的范围并向用户发送通知?
我正在用 Swift 编写代码。
英文:
There are three states for a app
- Foreground
- Background
- Terminated
iBeacons are working perfectly in Foreground and Background and sending notifications to the users.
In case, of terminated state, how can I detect users enter the range of iBeacon and send notification to the user?
I'm writing code in swift.
答案1
得分: 4
你可以使用iBeacon传输来在iOS上从终止状态下自动启动应用程序(无论是在关闭应用程序后还是在重新启动后的一分钟左右,一切都完全初始化后)。这对于“区域进入”(在一段时间内没有检测到任何信标后至少检测到一个信标)或“区域退出”(匹配区域的所有信标消失超过30秒)都有效。在理想情况下,信标检测后的自动启动发生在不到一秒钟内。
为了使此功能正常工作,您必须确保执行以下所有操作:
-
在
LocationManager
上调用startMonitoring(region: Region)
,并使用与您的信标匹配的CLBeaconRegion
对象。 -
在AppDelegate的
didFinishLaunching
方法中开始监控。如果在此方法返回之前不触发监控的启动,自动启动将无法正常工作。 -
从用户获取“始终”位置权限。如果应用程序未从用户那里获得“始终”权限,而只获得“在使用时”或“仅一次”位置权限,那么自动启动将无法正常工作。
-
在设置中打开位置和蓝牙。
-
等待位置服务准备就绪。如果您重新启动了手机,必须等待一分钟左右,直到位置服务完全初始化。如果在位置服务完全初始化之前出现或消失信标,自动启动将无法立即正常工作。
-
确保区域状态发生变化。为了在检测到信标时自动启动,iOS必须先检测到匹配
CLBeaconRegion
的所有信标已消失。在测试中,这意味着您需要让您的应用程序运行(最好在前台运行)至少30秒,附近没有匹配的信标,以确保iOS知道它在测试自动启动时是“外部”的。在测试过程中不这样做通常会导致错误的结论,即检测到信标的自动启动不起作用。
为了在未检测到信标时自动启动,您必须确保iOS在所有信标消失之前至少看到一个信标。请理解,iOS需要时间来确定所有信标都已消失。在理想情况下,这只需要30秒,但如果iOS处于低功耗状态并且没有进行蓝牙扫描,可能需要更长的时间。
这是一个深入描述自动启动工作原理的深度探讨。
英文:
You may use iBeacon transmissions to auto-launch an app from a terminated state on iOS (either after killing the app or a minute or so after reboot once everything is fully initialized.) This works for both a "region entry" (at least one beacon being detected after a period where none are detected) or a "region exit" (all beacons matching the region disappearing for > 30 seconds). Under ideal conditions, auto-launch on beacon detection happens in under one second.
In order for this to work, you must ensure that ALL of the following are done:
-
Call
startMonitoring(region: Region)
onLocationManager
with aCLBeaconRegion
object that matches your beacons. -
Start Monitoring in the
didFinishLaunching
method of the AppDelegate. If you do not trigger a start to monitoring before the return of this method, auto-launch won't work. -
Obtain "always" location permission from the user. If the app does not obtain "always" permission from the user and only gets "when in use" or "just once" location permission, then auto-launch won't work.
-
Location and Bluetooth must be turned on in settings.
-
Wait for Location Services to be ready. If you have rebooted the phone, you must wait for a minute or so before location services are fully initialized. If a beacon appears or disappears before location services are fully initialized, auto-launch won't work right away.
-
Make sure a region state change happens. In order to auto-launch upon detecting a beacon, iOS must have previously detected that all beacons matching the
CLBeaconRegion
had disappeared. In testing, this means that you need to let your app run (it's easiest to do this in the foreground) for at least 30 seconds with no matching beacons in the vicinity to ensure iOS knows it is "outside" the region before testing auto-launch on region entry. Failure to do this during testing often leads to the wrong conclusion that auto-launch on detection doesn't work.
In order to auto-launch upon not detecting a beacon, you must
similarly make sure that iOS sees at least one beacon before all
beacons disappear. Understand that it takes time for iOS to
determine that all beacons have disappeared. Under ideal conditions,
this is only 30 seconds, but it can be much longer if iOS is in a
low power state and is not doing any Bluetooth scanning.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论