ios - 显示 "Cannot find iBeacon"消息

标签 ios swift core-location ibeacon

我的问题很简单。在 viewDidLoad

中调用 startSearchingForSessions 之后,通过按下按钮调用 startSearchingForSessions 后,我想显示一条错误消息,即“无法找到 iBeacon”
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()

}

我已经实现了委托(delegate)错误方法,试图找到发生这种情况的位置,但到目前为止,在 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)")
}

谢谢!

最佳答案

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

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

关于ios - 显示 "Cannot find iBeacon"消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44537455/

相关文章:

swift - 即使设置了约束,UIView 也会被裁掉

ios - 如何将值从一个函数传递到另一个函数?

ios - MVP中的核心位置

iOS 设置(隐私 -> 定位服务 -> 选择我的应用 -> 更改允许访问)崩溃

ios - Xcode 4.3.1 纹理工具丢失了吗?

ios - 使用 Google 标签管理器阻止 firebase 事件 | iOS

ios - 如何使用 Swift 在文本字段(从右到左)输入货币格式?

objective-c - 显示视频和图像

ios - 如何移动 UIView 并使用 Swift 以编程方式放置新的 UIView?

ios - 让多个 UIButton 共享相同外观的最佳方法