从框架背景中的信标检测

时间:2016-10-07 08:43:46

标签: ios background frameworks ibeacon detection

我有一个框架,它拥有所有信标检测逻辑和一个设置和拆除框架的示例应用程序。我想在应用程序被杀后获取区域进入和退出通知。当逻辑在应用程序中时,我能够从应用程序收到通知。但是当逻辑处于框架中时,我不会收到通知。我做错了什么?

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)
        }
    }
}

1 个答案:

答案 0 :(得分:1)

为了防止将停止监视的垃圾收集,locationManager需要是一个类变量,初始化必须在方法内部进行。像这样:

let locationManager: CLLocationManager!

func registerForBeaconNotifications() {
    self.locationManager = CLLocationManager()
    ...
相关问题