首页 > 解决方案 > 在后台或应用未运行时监控 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)
}

标签: iosswiftbackgroundibeacon

解决方案


应用程序以 2 种方式与信标交互: 1) 监控:当应用程序进入/退出区域范围时触发的操作。无论应用程序是否正在运行、暂停或终止(如果应用程序在进入/退出事件发生时未运行,iOS 将启动它到后台几秒钟以处理该事件)。2) 测距:根据与信标的接近程度触发动作。这仅在应用程序运行时有效。

因此,为了让 iOS 在我们的应用程序未运行时唤醒它,您可以使用监控。但是监控有一些限制。它只识别进入/退出事件,不提供关于哪个确切信标触发了事件的信息(仅提供了哪个区域),也没有提供接近度估计。但是,您可以使用 iOS 授予您的应用程序处理进入/退出事件的时间来运行测距几秒钟,并了解哪些确切的信标在范围内,以及它们的接近程度。


推荐阅读