CLLocationManager:没有调用 didChangeAuthorization 和 didRangeBeacons

CLLocationManager: didChangeAuthorization and didRangeBeacons is not getting called

我正在开发一个框架,其中包含信标测距和监控的所有逻辑。

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

部分代码如下:

这是框架的主要class(从测试应用调用):

//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 是可以正常工作的网络函数。 测试应用程序具有用户的位置权限和所需的所有功能。

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

问题是 locationManager 对象的生命周期。

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

public class Kanban: NSObject, CLLocationManagerDelegate {
    var apiKey: String!
    var userID: String!
    var beaconsJson: [BeaconItem]?

    let locationManager = CLLocationManager()
    //...
}

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

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