ios - 在后台或应用未运行时监控 Bluecats 信标
问题描述
我正在为我的应用程序使用 bluecats 信标。我想在用户应用程序未运行时检测用户是否进入信标区域,我想显示用户已进入信标区域的本地通知
这是我的代码:App Delegate 类
var beaconManager = BCBeaconManager()
var beacon_region: BCBeaconRegion!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
beaconManager.delegate = self
self.requestAuthorizationForLocalNotifications()
BlueCatsSDK.startPurring(withAppToken: "7d17d7cb-8a26-4b8b-999f-9b031deab7a5", completion: { (BCStatus) -> Void in
let appTokenVerificationStatus: BCAppTokenVerificationStatus = BlueCatsSDK.appTokenVerificationStatus()
if (appTokenVerificationStatus == .notProvided || appTokenVerificationStatus == .invalid){
}
if (!BlueCatsSDK.isLocationAuthorized()){
BlueCatsSDK.requestAlwaysLocationAuthorization()
}
if (!BlueCatsSDK.isNetworkReachable()){
}
if (!BlueCatsSDK.isBluetoothEnabled()){
let alert = UIAlertController(title: "Turn On Bluetooth", message: "This App requires that Bluetooth is enabled to process your orders", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Don’t Allow", style: UIAlertAction.Style.cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Allow", style: UIAlertAction.Style.default, handler: nil))
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
}
})
self.beaconManager.startMonitoringBeaconRegion(beacon_region)
return true
}
// On Exit
func beaconManager(_ beaconManager: BCBeaconManager!, didExitSite site: BCSite!) {
PresentNotifications(title: "You exited the region")
}
// On Enter
func beaconManager(_ beaconManager: BCBeaconManager!, didEnter site: BCSite!) {
PresentNotifications(title: "You Entered the region")
}
func beaconManager(_ beaconManager: BCBeaconManager!, didDetermineState state: BCSiteState, for site: BCSite!) {
if state == BCSiteState.inside{
PresentNotifications(title: "You are inside the region")
}
else if state == BCSiteState.outside{
PresentNotifications(title: "You are outside the region")
}
else{
return
}
}
func applicationDidEnterBackground(_ application: UIApplication) {
self.beaconManager.startMonitoringBeaconRegion(beacon_region)
}
解决方案
应用程序以 2 种方式与信标交互: 1) 监控:当应用程序进入/退出区域范围时触发的操作。无论应用程序是否正在运行、暂停或终止(如果应用程序在进入/退出事件发生时未运行,iOS 将启动它到后台几秒钟以处理该事件)。2) 测距:根据与信标的接近程度触发动作。这仅在应用程序运行时有效。
因此,为了让 iOS 在我们的应用程序未运行时唤醒它,您可以使用监控。但是监控有一些限制。它只识别进入/退出事件,不提供关于哪个确切信标触发了事件的信息(仅提供了哪个区域),也没有提供接近度估计。但是,您可以使用 iOS 授予您的应用程序处理进入/退出事件的时间来运行测距几秒钟,并了解哪些确切的信标在范围内,以及它们的接近程度。
推荐阅读
- python - python - 如何从元组中获得持久哈希?
- c# - HasThis 和 ExplicitThis 调用约定
- android - 如何修复“必需的 MutableLiveData
但“SwitchMap”被推断为 LiveData " 使用 Transformations.switchMap 时? - swift - Swift 4:“(SomeUIViewController)->()->(SomeUIViewController)”类型的值没有成员“someMemberItem”
- python - 从预先计算的一组绘图中创建动画:ArtistAnimation
- react-native - 每次系统重启时都需要设置 JAVA_HOME 变量吗?
- database - 在 Laravel 5.7 中以链式关系播种
- macos - 我的 Mac 上的 Spyder 最近停止工作,并在我启动它后立即向终端发送错误消息
- django - 用法:py [-x] [-l] [-c PRE_CMD] [-C POST_CMD] [-V] [-h] [表达式]
- c++ - 访问 std::list 元素会被核心转储