当应用程序在iOS 11中被用户杀死时的地理围栏

时间:2019-07-03 13:09:54

标签: ios swift bluetooth-lowenergy

在iOS 11中用户杀死App时的地理围栏

iOS 11.我已经正确设置了CLLocationManager,它侦听区域的进入/退出。一切正常。但是当我杀死我的应用程序并进入一个区域时,我收到了本地通知(在didEnterRegion方法中定义),但无法在后台重新启动。我以为它将在后台启动该应用程序,执行didEnterRegion:并在一段时间后再次杀死自己。我需要做的就是,BLE扫描并在didEnterRegion内调用BLE委托:

请让我知道这有可能吗?

func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {

        if region is CLCircularRegion {
            debugPrintLog(s: ">>>>>>>>>>>>>>>>>>>>>didEnterRegion")
            outSideFromGeoReasion(identifire: region.identifier)
        }
    }

    // called when user Enters a monitored region
    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {

        debugPrintLog(s: ">>>>>>>>>>>>>>>>>>>>>didEnterRegion")
        if region is CLCircularRegion {
            insideInGeoResion(identifire: region.identifier)
            Shared.sharedInstance.isStopBleScanFromGeo = false
        }
    }

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

        switch state {
        case .inside:
            print("inside doorname :-" + region.identifier)
            insideInGeoResion(identifire: region.identifier)
            Shared.sharedInstance.isStopBleScanFromGeo = false
            print("inside")
        case .outside:

            outSideFromGeoReasion(identifire: region.identifier)
            print("outside doorname :-" + region.identifier)
            print("outside")
        default:

            print("Unknown")
        }
    }

    func insideInGeoResion(identifire:String){

        let stringArray :[String] = identifire.components(separatedBy: "()");

        let dooId:Int = Int(stringArray[0])!
        let inside: Int = DataBaseManager.sharedInstance.fatchInsideGeofence(doorId: dooId)
        if !(inside == 2){

            print(">>>>>>>>>>>>>>m Door Home identifire", identifire )
            Shared.sharedInstance.fireLocalPush(title:"Geofence", subtitle: stringArray[1] + " in Home mode", timeInterval:1, identifier: identifire + "Entry", enableSound: false)
            DataBaseManager.sharedInstance.updateInsideGeofancing(insideDB: dooId, value: 2)
        }
        if !(Shared.sharedInstance.centralManager != nil) {
            Shared.sharedInstance.centralManager = CBCentralManager()
        }else{
            if Shared.sharedInstance.centralManager?.state == .poweredOn {
                if let manager = Shared.sharedInstance.centralManager, !manager.isScanning {
                    manager.scanForPeripherals(withServices: [Shared.sharedInstance.serviceUUID], options:[CBCentralManagerScanOptionAllowDuplicatesKey:false])
                    Shared.sharedInstance.isStopBleScanFromGeo = true
                }

            }
        }
    }


    func outSideFromGeoReasion(identifire:String) {

        let stringArray :[String] = identifire.components(separatedBy: "()");
//        print(">>>>>>>>>>>>>>m Door Away identifire", identifire )
        let dooId:Int = Int(stringArray[0])!
        let outside: Int = DataBaseManager.sharedInstance.fatchInsideGeofence(doorId: dooId)
        if !(outside == 1){
             print(">>>>>>>>>>>>>>m Door Away identifire", identifire )
            Shared.sharedInstance.fireLocalPush(title:"Geofence", subtitle: stringArray[1] + " in Away mode", timeInterval: 1, identifier: identifire + "Exit", enableSound: false)
            DataBaseManager.sharedInstance.updateInsideGeofancing(insideDB: dooId, value: 1)
        }
        DataBaseManager.sharedInstance.devicesDetail { (savedoor) in
            let existLock =  savedoor.filter({ (object) -> Bool in
                return (object.value(forKey: "inside_Geofance") as! Int) == 2
            })
            if !(existLock.count > 0) {
                if Shared.sharedInstance.centralManager != nil {
                    if Shared.sharedInstance.centralManager?.state == .poweredOn {
                        Shared.sharedInstance.centralManager?.stopScan()
                        Shared.sharedInstance.isStopBleScanFromGeo = true
                    }
                }
            }
        }
    }

仅在被杀死状态下启动BLE扫描,并调用CBCentralManager的所有委托。以及如何检查出血扫描。

0 个答案:

没有答案