【问题标题】:CLLocationManager: didChangeAuthorization and didRangeBeacons is not getting calledCLLocationManager:没有调用 didChangeAuthorization 和 didRangeBeacons
【发布时间】:2019-09-19 18:23:47
【问题描述】:

我正在开发一个框架,该框架具有信标测距和监控的所有逻辑。

CLLocationManager 的所有回调都不起作用。这仅在我使用框架时发生。如果我将所有逻辑转移到测试应用程序,它就可以工作。

以下是部分代码:

这是框架的主类(从测试应用程序调用):

//Test App
let miniK = Kanban()
miniK.startBeaconSearch(forApiKey: "apikeytest", userID: "1")
public class Kanban: NSObject {


    public override init() {
        super.init()
    }

    public func startBeaconSearch(forApiKey apikey: String, userID: String){
        let beaconController = BeaconController.init(apikey: apikey, userId: userID)
        beaconController.startScanning()
    }

}

信标控制器:

import Foundation
import CoreLocation
import CoreBluetooth

class BeaconController : NSObject, CLLocationManagerDelegate {
    var locationManager: CLLocationManager!
    var apiKey: String!
    var userID: String!
    var beaconsJson : [BeaconItem]?

    public override init(){
        super.init()
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
    }

    public init(apikey:String, userId:String){
        super.init()
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
        apiKey = apikey
        userID = userId
        self.loadBeacons()
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)
    {
        if status == .authorizedAlways
        {
            if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self){
                if CLLocationManager.isRangingAvailable() {
                    startScanning()
                }
            }
        }
    }


    func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
        print("didRangeBeacons")
        if beacons.count > 0 {
            self.beaconReached(uuid: beacons[0].proximityUUID, minor: beacons[0].minor, major: beacons[0].major)
        }
    }

    func locationManager(_ manager: CLLocationManager, rangingBeaconsDidFailFor region: CLBeaconRegion, withError error: Error) {
        print(error)
        print(error.localizedDescription)
    }

    public func startScanning() {
        print("StartScanning")
        sleep(5)
        //let beacon = beaconsJson![0]

        for beacon in beaconsJson! {
            print(beacon.BeaconUUID)
            let identifier = "iBeacon" + beacon.BeaconMajor.description + beacon.BeaconMinor.description
            let uuid = UUID(uuidString: beacon.BeaconUUID)!
            let beaconRegion = CLBeaconRegion(proximityUUID: uuid, major: CLBeaconMajorValue(beacon.BeaconMajor), minor: CLBeaconMinorValue(beacon.BeaconMinor), identifier: identifier)
            //beaconRegion.notifyOnEntry = true
            locationManager.requestState(for: beaconRegion)
            locationManager.startMonitoring(for: beaconRegion)
            locationManager.startRangingBeacons(in: beaconRegion)
        }
        print(locationManager.rangedRegions)
    }
}

loadBeaconsbeaconReached 是可以正常工作的网络功能。 测试应用具有用户的位置权限和所需的所有功能。

当我打印范围区域时,信标正确显示。

【问题讨论】:

    标签: swift frameworks core-location ibeacon


    【解决方案1】:

    问题在于locationManager 对象的生命周期。

    首先,您必须像这样将locationManager 定义为类对象。

    public class Kanban: NSObject, CLLocationManagerDelegate {
        var apiKey: String!
        var userID: String!
        var beaconsJson: [BeaconItem]?
    
        let locationManager = CLLocationManager()
        //...
    }
    

    然后,在框架的实现中,它也必须是一个类对象,像这样。

    struct ContentView: View {
        @State private var selection = 0
        let miniK = Kanban()
        //...
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-11
      • 1970-01-01
      • 2013-12-03
      • 1970-01-01
      • 2011-11-18
      相关资源
      最近更新 更多