显示 "Cannot find iBeacon" 消息

Display "Cannot find iBeacon" message

我的问题很简单。我想显示一条错误消息,即 "Cannot find iBeacon" 如果 iBeacon 监控失败,在通过 viewDidLoad

中调用后按下按钮调用 startSearchingForSessions 之后
override func viewDidLoad() {
    super.viewDidLoad()

    self.locationManager = CLLocationManager()
    if self.locationManager.responds(to: #selector(CLLocationManager.requestWhenInUseAuthorization)) {
        self.locationManager.requestWhenInUseAuthorization()
    }
    self.locationManager.delegate = self
    self.locationManager.pausesLocationUpdatesAutomatically = false

    let uuid = UUID(uuidString: "869A6E2E-AE14-4CF5-8313-8D6976058A7A")
    self.beaconRegion = CLBeaconRegion(proximityUUID: uuid!, identifier: "com.dejordan.myapp"
    startSearchingForSessions()

}

func startSearchingForSessions() {

    // Start looking for the beacons with that UUID and Identifier.
    self.locationManager.startMonitoring(for: self.beaconRegion)
    self.locationManager.startRangingBeacons(in: self.beaconRegion)
    self.locationManager.startUpdatingLocation()

}

并这样处理找到的信标:

// Required by the Location Manager.
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
    self.locationManager.startRangingBeacons(in: self.beaconRegion)
}

func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
    if state == CLRegionState.outside {
        print("Cannot Find Beacon")
    }
}

// Required by the Location Manager.
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
    self.locationManager.stopRangingBeacons(in: self.beaconRegion)
}

// This is called if any beacons are found.
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {

    var result = Array<CLBeacon>()
    for beacon in beacons {
        result.append(beacon)
    }

    foundBeacons = result
    // If we found any, we need to see
    // what class they belong to based on information
    // from Parse.
    self.identifyFoundBeacons()

    // We can stop looking for beacons now.
    self.locationManager.stopMonitoring(for: self.beaconRegion)
    self.locationManager.stopRangingBeacons(in: self.beaconRegion)
    self.locationManager.stopUpdatingLocation()

}

我已经实现了委托错误方法,试图找到发生这种情况的地方,但到目前为止,在浏览 iBeacon 上的大量文档时,我一无所获。

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    print("Location manager failed: \(error.localizedDescription)")
}

func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
    print("Failed monitoring region: \(error.localizedDescription)")
}

谢谢!

如果您只是想知道何时未检测到信标(相对于查找信标时出现低级别故障),则只需使用以下委托方法:

public func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
  if state == CLRegionState.outside {
    print("Cannot find beacon")
  }
}

有趣的是,didRangeBeacons 在委托方法中

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

使用空数组 [CLBeacon] 调用,其中我可以使用 beacons 数组大小来确定是否找到任何信标。

出乎我的意料,但这解决了我的问题!