CLLocationManager:没有调用 didChangeAuthorization 和 didRangeBeacons

Posted

技术标签:

【中文标题】CLLocationManager:没有调用 didChangeAuthorization 和 didRangeBeacons【英文标题】:CLLocationManager: didChangeAuthorization and didRangeBeacons is not getting called 【发布时间】: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 是可以正常工作的网络功能。 测试应用具有用户的位置权限和所需的所有功能。

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

【问题讨论】:

【参考方案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()
    //...


【讨论】:

以上是关于CLLocationManager:没有调用 didChangeAuthorization 和 didRangeBeacons的主要内容,如果未能解决你的问题,请参考以下文章

一段时间后没有调用 CLLocationManager didUpdateToLocation

CLLocationManager 未在 iOS 10.2 [Swift] 中调用 didUpdateLocation()

在 iOS 11 和 Xcode 9.1 中未调用 CLLocationManager 的 didUpdateLocations 委托 [重复]

CLLocationManager 和 distanceFilter?

CLLocationManager 从不调用委托方法[重复]

故意调用 CLLocationManager 委托的 didFailWithError: