在后台或未运行应用程序时监视蓝猫信标

时间:2018-06-28 09:40:38

标签: ios swift background ibeacon

我正在为我的应用程序使用蓝猫信标。我想检测用户应用未运行时用户是否进入信标区域,我想显示用户已进入信标区域的本地通知

这是我的代码: 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)
}

1 个答案:

答案 0 :(得分:0)

应用程序通过两种方式与信标交互: 1)监控:应用程序进入/退出区域范围时触发的动作。无论应用程序是正在运行,已暂停还是已终止,它都可以工作(如果在进入/退出事件到来时该应用程序未运行,iOS会将其启动到后台运行几秒钟以处理该事件)。 2)测距:根据与信标的接近程度触发操作。仅当应用程序运行时,它才起作用。

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

相关问题