从框架后台进行信标检测 [英] Beacon detection in background from framework
本文介绍了从框架后台进行信标检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含所有信标检测逻辑的框架和一个用于建立和拆除框架的示例应用程序。我想在应用被杀死后获取区域进入和退出通知。当逻辑在应用程序中时,我能够从应用程序中获取通知。但是当逻辑在框架中时,我不会收到通知。我在做什么错?
I have a framework which had all beacon detection logic and a sample app which sets up and tears down framework. I want to get region enter and exit notifications after app is killed. I am able to get notifications from app when logic is in app. But when the logic is in framework I don't get notifications. What am I doing wrong?
import UIKit
import CoreLocation
extension AppDelegate: CLLocationManagerDelegate {
func registerForBeaconNotifications() {
let locationManager = CLLocationManager()
let region = CLBeaconRegion(proximityUUID: UUID(uuidString: "83f9daec-4cae-54f1-b64e-846f12345d05")!, major: 10, minor: 10, identifier: "iPhone 6 Beacon")
locationManager.delegate = self
region.notifyOnEntry = true
region.notifyOnExit = true
region.notifyEntryStateOnDisplay = true
locationManager.startMonitoring(for: region)
locationManager.startRangingBeacons(in: region)
// Register for showing notification alerts
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: .alert, categories: nil))
}
func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
let notification = UILocalNotification()
switch state {
case .inside:
notification.alertBody = "Entered region"
UIApplication.shared.presentLocalNotificationNow(notification)
case .outside:
notification.alertBody = "Exited region"
UIApplication.shared.presentLocalNotificationNow(notification)
default:
notification.alertBody = "Region unknown"
UIApplication.shared.presentLocalNotificationNow(notification)
}
}
}
推荐答案
为了防止垃圾收集,它将停止监视, locationManager
必须是一个类变量,并且初始化必须在方法内部进行。像这样:
In order to prevent garbage collection which will stop monitoring, locationManager
needs to be a class variable, and the initialization must take place inside a method. Like this:
let locationManager: CLLocationManager!
func registerForBeaconNotifications() {
self.locationManager = CLLocationManager()
...
这篇关于从框架后台进行信标检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文