应用程序快速杀死时检测iBeacon

时间:2017-07-24 07:40:33

标签: ios iphone swift3 core-location ibeacon

如果iPhone使用UUID检测到(背景中的信标),我需要做一些事情:" 3E06945C-F54A-4BE4-ABB2-79764DA6AC6F"所以我想在AppDelegate中这样做:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert], categories: nil))

    initBeaconManager()
    //Fabric.with([Crashlytics.self])
    //Fabric.with([Appsee.self])
    return true
}

func applicationWillTerminate(_ application: UIApplication) {
    print("Application will terminate")
}

func applicationDidEnterBackground(_ application: UIApplication) {
    isInForeground = false
    isInBackground = true
    extendBackgroundTime()
}

func applicationDidBecomeActive(_ application: UIApplication) {
    isInForeground = true
    isInBackground = false
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "initCarPlaceNotification"), object: nil)
}
func extendBackgroundTime() {
    if backgroundTask != UIBackgroundTaskInvalid {
        // Still running
        return
    }
    else {
        backgroundTask = UIApplication.shared.beginBackgroundTask(withName: "Ranging", expirationHandler: { () in
            // Background task expired by iOS
            UIApplication.shared.endBackgroundTask(self.backgroundTask)
            self.backgroundTask = UIBackgroundTaskInvalid
        });

        DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async { //
            if !self.isInForeground {
                while true {
                    Thread.sleep(forTimeInterval: 1)
                }
            }
        }
    }

func initBeaconManager() {
    let beaconsUUIDs = ["1":"3E06945C-F54A-4BE4-ABB2-79764DA6AC6F"]

    locationManager = CLLocationManager()
    if (locationManager!.responds(to: #selector(CLLocationManager.requestAlwaysAuthorization))) {
        locationManager!.requestWhenInUseAuthorization()
    }
    locationManager!.delegate = self
    locationManager!.pausesLocationUpdatesAutomatically = true

    for (key, value) in beaconsUUIDs {
        let beaconUUID:UUID = UUID(uuidString: value)!
        let beaconRegion = CLBeaconRegion(proximityUUID: beaconUUID, identifier: "reg-\(key)")
        locationManager!.startMonitoring(for: beaconRegion)
        locationManager!.startRangingBeacons(in: beaconRegion)
    }

    locationManager!.startUpdatingHeading()
    locationManager!.startUpdatingLocation()

}
extension AppDelegate {
    func rangeForAllBeacons(_ beacons:[CLBeacon]) {
    if beacons[0].proximityUUID == UUID(uuidString: "3E06945C-F54A-4BE4-ABB2-79764DA6AC6F") {
        displayNotification("work fine..")
    }
}

extension AppDelegate:CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
    if !isInForeground {
        extendBackgroundTime()
    }

    print(beacons)

    for beacon in beacons {
        if beacon.rssi < -20 {
            nearbyBeacons.append(beacon)
        }
    }

    if firstRegionDetected == "" {
        firstRegionDetected = region.identifier
    } else if firstRegionDetected == region.identifier {
        rangeForAllBeacons(nearbyBeacons)
        nearbyBeacons = []
    }
}

func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
    if isInBackground {
        extendBackgroundTime()
    }
}

但它不起作用。在info.plist上我使用

Privacy - Location Always Usage Description

Privacy - Location When In Use Usage Description

0 个答案:

没有答案