【问题标题】:Display "Cannot find iBeacon" message显示“找不到 iBeacon”消息
【发布时间】:2017-11-16 04:10:28
【问题描述】:

我的问题很简单。如果 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)")
}

谢谢!

【问题讨论】:

  • 根据您的代码 func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) { print("Failed monitoring region: (error.localizedDescription)") } 是正确的在信标监视器失败中检测失败
  • 您可以使用 UIAlertController 来显示消息。确保显示 UIAlertController 的调用在 UI 线程(主线程)中:stackoverflow.com/documentation/ios/874/…
  • 你有没有看到requestWhenInUseAuthorization() 的对话框,你确定它被授予了吗?您还必须打开手机的蓝牙和位置信息。
  • 是的,我找到 iBeacon 没有任何问题,我只是无法捕捉到它找不到找到 iBeacon
  • 令我惊讶的是,didRangeBeacons 是我正在寻找的方法,即使它返回一个空数组,我也可以使用大小来确定是否找到任何信标。无论如何感谢您的帮助!

标签: ios swift core-location ibeacon


【解决方案1】:

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

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

【讨论】:

  • hmmm.... 它似乎没有抓住那里。我在前面发布的.start 函数上附加了一个按钮,几秒钟后每次按下都没有任何反应
  • 您能否扩充您的问题以显示您设置 LocationManager 和委托的位置的完整代码?您是否请求了位置许可?你能显示那个代码吗?
【解决方案2】:

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

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

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

不是我的预期,但这解决了我的问题!

【讨论】:

    猜你喜欢
    • 2018-03-10
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 2012-12-16
    相关资源
    最近更新 更多