swift - 在前台、后台和杀死(从后台删除)应用程序中检测信标

标签 swift ibeacon locationmanager kontakt.io

我正在研究前台、后台和杀死(从后台删除)应用程序中的信标检测。我有 kontakt 信标。我已经实现了位置管理器委托(delegate)方法,但我无法在前台检测到信标和背景甚至没有调用方法 didEnterRegiondidExitRegion 。对于解决方案,我将位置管理器委托(delegate)与 KTKDevicesManagerDelegate 合并。我无法在位置管理器中检测到信标,所以我尝试在位置管理器方法的 didRangeBeacons 中实现我启动 KTKDeviceManagerDelegate 的 devicesManager.startDevicesDiscovery() 那个时候检测前景和背景中的信标,但我不能当我们杀死应用程序时检测信标。我已经在 Info.plist 中添加了 NSBluetoothPeripheralUsageDescription 、 NSLocationAlwaysUsageDescription 、 NSLocationWhenInUseUsageDescription 、 bluetooth-central 、 fetch 、 location 。 我在 didFinishLaunchingWithOptions requestAlwaysAuthorization(),requestWhenInUseAuthorization() 上添加了以下内容。

    //MARK:- Variable initializers
    var devicesManager: KTKDevicesManager!
    var locationManager = CLLocationManager()
    let region = CLBeaconRegion(proximityUUID: NSUUID(uuidString: "********")! as UUID, identifier: "detected")

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

        Kontakt.setAPIKey(“*******************”)

         self.locationManager.delegate = self
        devicesManager = KTKDevicesManager(delegate: self)
//When bluetooth Is on that time
        devicesManager.startDevicesDiscovery(withInterval: 3)

}

//MARK: - KTKDevicesManagerDelegate Method

extension AppDelegate: KTKDevicesManagerDelegate {

    func devicesManager(_ manager: KTKDevicesManager, didDiscover devices: [KTKNearbyDevice]) {

        for device in nearbyDevices {
            if let uniqueID = device.uniqueID {
                print("Detected a beacon \(uniqueID)")
            } else {
                print("Detected a beacon with an unknown unique ID")
                devicesManager.stopDevicesDiscovery()
            }
        }

    }

    func devicesManagerDidFail(toStartDiscovery manager: KTKDevicesManager, withError error: Error?) {

        print("Discovery did fail with error: \(error)")
        devicesManager.stopDevicesDiscovery()
      }

}

//MARK: - Location Manager Method

extension AppDelegate: CLLocationManagerDelegate {

    private func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        print("STATUS CHANGED: \(status.rawValue)")
        if status == .authorizedAlways  {
            print("YAY! Authorized!")
            locationManager.startMonitoring(for: region)

        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        print("update location")

        locationManager.startMonitoring(for: region)
        locationManager.startRangingBeacons(in: region)
   }



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

        print("The monitored regions are: \(manager.monitoredRegions)")
        locationManager.startMonitoring(for: region)
        locationManager.startRangingBeacons(in: region as! CLBeaconRegion)

    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Location Manager failed with the following error: \(error)")
    }

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

       //  print("beacons : \(beacons.count)")

        devicesManager.startDevicesDiscovery()
       //So called  didDiscover method of KTKDevicesManagerDelegate method

    }

    func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
        print("FAIL!")
    }

    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
        print("Region Entered")

        locationManager.startMonitoring(for: region)
        locationManager.startRangingBeacons(in: region as! CLBeaconRegion)

    }


    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
        print("Region exited")

        locationManager.startMonitoring(for: region)
        locationManager.startRangingBeacons(in: region as! CLBeaconRegion)

    }

}

最佳答案

您必须在 NSLocationAlwaysUsageDescription 中添加并允许位置.如果你当时在信标区didEnterRegion那个时候方法执行并离开信标区域didExitRegion执行的方法,那时我们开始和停止设备发现。我们可以在前台、后台、锁定手机和处于终止状态的应用程序中检测信标。

按照下面给出的方法修改您的方法。

    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
            print("Region Entered")
            devicesManager.startDevicesDiscovery()


        }


        func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
            print("Region exited")
            devicesManager.stopDevicesDiscovery()    
        }

关于swift - 在前台、后台和杀死(从后台删除)应用程序中检测信标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51960205/

相关文章:

swift - 在 Swift 中使用索引映射或减少

ios - 简单的演示接近事件 iOS 应用程序不工作(Estimote 信标)

java - Android 中 LocationManager 的 requestLocationUpdates 到底是如何工作的?

swift - 在 Swift 中比较枚举

ios - 使用自定义导航栏在导航堆栈中的 Controller 之间进行转换

swift - JSQMessageAvatarImageFactory错误

android - 如何使用 LAC(位置区号)获取位置名称

ios - 检测不到区域内所有Estimotes时如何在didExitRegion中添加条件

android - 我们可以使用 altbeacon 获得监视信标的 eddystone uid 吗?

使用 CLLocationManager 的 Swift 3 信标监控