didEnterRegion func调用了两次

时间:2016-10-15 21:56:10

标签: ios swift core-location appdelegate

我在AppDelegate中为ibeacon运行locationManager didEnterRegion。 当ibeacon进入区域时,我会打印三次日志。 我能做什么?有没有解决这个问题? 感谢

import UIKit
import CoreLocation

@UIApplicationMain

class elegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var locationManager: CLLocationManager?
    var lastProximity: CLProximity?
    var lastUUID: NSUUID!
    var lastBeacanIdentifier:String = ""
    var lastMajorValue: NSNumber = 0.0
    var lastMinorValue: NSNumber = 0.0


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

        let uuidString = "A18F1103-8247-4555-B640-C126EAD04954"
        let beaconIdentifier = "co.Company"
        let beaconUUID:NSUUID = NSUUID(uuidString: uuidString)!
        let beaconRegion:CLBeaconRegion = CLBeaconRegion(proximityUUID: beaconUUID as UUID, identifier: beaconIdentifier)

        locationManager = CLLocationManager()

        locationManager!.delegate = self
        locationManager!.pausesLocationUpdatesAutomatically = false
        locationManager!.startMonitoring(for: beaconRegion)
        locationManager!.startRangingBeacons(in: beaconRegion)
        locationManager!.startUpdatingLocation()

        return true
    }  
}

extension elegate: CLLocationManagerDelegate {
    func sendLocalNotificationWithMessage(message: String!) {
        let notification:UILocalNotification = UILocalNotification()
        notification.alertBody = message
        UIApplication.shared.scheduleLocalNotification(notification)
    }

    func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {

       // NSLog("didRangeBeacons");
        var message:String = ""

        if(beacons.count > 0) {
            let nearestBeacon:CLBeacon = beacons[0] 
            if(nearestBeacon.proximity == lastProximity ||
                nearestBeacon.proximity == CLProximity.unknown) {
                return;
            }

            lastProximity = nearestBeacon.proximity;
            lastMajorValue = nearestBeacon.major;
            lastMinorValue = nearestBeacon.minor;
            lastUUID = nearestBeacon.proximityUUID as NSUUID!;


            switch nearestBeacon.proximity {
            case CLProximity.far:
                message = "You are far away from the beacon";
                print(lastMajorValue)
                print(lastMinorValue)
                print(lastUUID)
            case CLProximity.near:
                message = "You are near the beacon";
                print(lastMajorValue)
                print(lastMinorValue)
                print(lastUUID)
            case CLProximity.immediate:
                message = "You are in the immediate proximity of the beacon";
                print(lastMajorValue)
                print(lastMinorValue)
                print(lastUUID)
            case CLProximity.unknown:
                return
            }
        } else {
           // message = "No beacons are nearby"
        }

        NSLog("%@", message)
        sendLocalNotificationWithMessage(message: message)
    }
    func locationManager(_ manager: CLLocationManager,didEnterRegion region: CLRegion) {
        manager.startRangingBeacons(in: region as! CLBeaconRegion)
        manager.startUpdatingLocation()
        NSLog("You entered the region")
    }

    func locationManager(_ manager: CLLocationManager,
                         didExitRegion region: CLRegion) {
        manager.stopRangingBeacons(in: region as! CLBeaconRegion)
        manager.stopUpdatingLocation()

        //NSLog("You exited the region")
        //sendLocalNotificationWithMessage(message: "You exited the region")
    }
}

0 个答案:

没有答案