从框架的背景中的信标检测
我有一个框架,有所有的信标检测逻辑和一个示例应用程序设置和拆卸框架。 我想在应用程序被杀后获得区域进入和退出通知。 当逻辑处于应用程序中时,我可以从应用程序获取通知。 但是,当框架中的逻辑我没有得到通知。 我究竟做错了什么?
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
需要是一个类variables,初始化必须在一个方法内部进行。 喜欢这个:
let locationManager: CLLocationManager! func registerForBeaconNotifications() { self.locationManager = CLLocationManager() ...