Swift CLLocationManager:未调用didChangeAuthorization和didRangeBeacons

Swift CLLocationManager:未调用didChangeAuthorization和didRangeBeacons,swift,frameworks,core-location,ibeacon,Swift,Frameworks,Core Location,Ibeacon,我正在开发一个框架,其中包含了信标测距和监控的所有逻辑 CLLocationManager的所有回调都不起作用。这只有在我使用框架时才会发生。如果我将所有逻辑传输到测试应用程序,它就会工作 以下是部分代码: 这是框架的主要类(从测试应用程序调用): beacontroller: import Foundation import CoreLocation import CoreBluetooth class BeaconController : NSObject, CLLocationManag

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

CLLocationManager
的所有回调都不起作用。这只有在我使用框架时才会发生。如果我将所有逻辑传输到测试应用程序,它就会工作

以下是部分代码:

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

beacontroller:

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)
    }
}
loadbeacon
beaconreach
是正常工作的网络功能。 测试应用程序具有用户的位置权限和所需的所有功能


打印范围内的区域时,信标显示正确。

问题在于
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()
    //...
}

然后,在框架的实现中,它也必须是一个类对象,如下所示

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()
    //...
}


问题是
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()
    //...
}

然后,在框架的实现中,它也必须是一个类对象,如下所示

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()
    //...
}