【发布时间】:2021-12-29 21:56:51
【问题描述】:
我正在使用 iBeacon 进行测试,以便在被杀死后在 iOS 应用中执行一些与蓝牙相关的任务。
实际上效果很好,但我还是很好奇它是如何工作的。
这是我使用的代码。
private func startMonitoring() {
if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self) {
self.log("startMonitoring")
let region = CLBeaconRegion(...)
self.locationManager.startMonitoring(for: region)
}
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
if let region = region as? CLBeaconRegion {
if CLLocationManager.isRangingAvailable() {
self.log("didEnterRegion")
self.locationManager.startRangingBeacons(satisfying: region.beaconIdentityConstraint)
}
}
}
func locationManager(_ manager: CLLocationManager, didRange beacons: [CLBeacon], satisfying beaconConstraint: CLBeaconIdentityConstraint) {
if !beacons.isEmpty {
self.log("didRange")
self.doSomething()
}
}
我在应用程序最初启动时调用了一次startMonitoring(),并使用了两个CLLocationManagerDelegate 方法。我还添加了log() 进行测试。
我希望在我杀死应用程序后,首先调用didEnterRegion,然后调用didRange,最后执行任务。
但事实证明,我只看到了 3 条关于“startMonitoring”的日志,这意味着(我猜)iBeacon 以某种方式称为 startMonitoring()。
这怎么可能?为什么应用程序不调用委托方法,为什么它甚至运行良好?
【问题讨论】:
标签: ios swift core-location core-bluetooth ibeacon