Ios “显示”;找不到iBeacon“;消息

Ios “显示”;找不到iBeacon“;消息,ios,swift,core-location,ibeacon,Ios,Swift,Core Location,Ibeacon,我的问题很简单。在调用viewDidLoad后,我想在调用startSearchingForSessions后,通过按下按钮显示一条错误消息,即如果iBeacon监控失败,则显示“找不到iBeacon” override func viewDidLoad() { super.viewDidLoad() self.locationManager = CLLocationManager() if self.locationManager.responds(to: #sele

我的问题很简单。在调用
viewDidLoad
后,我想在调用
startSearchingForSessions
后,通过按下按钮显示一条错误消息,即如果iBeacon监控失败,则显示“找不到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()

}

谢谢大家!

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

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]
s的空数组调用,其中我可以使用
信标
数组大小来确定是否找到任何信标


不是我所期望的,但这解决了我的问题

根据您的代码func locationManager(manager:CLLocationManager,monitoringDidFailFor region:CLRegion?,withError error:error){print(“失败的监视区域:(error.localizedDescription)”)}正确地检测到信标监视器故障您可以使用UIAlertController显示消息。确保显示UIAlertController的调用位于UI线程(主线程)中:您是否曾看到
requestWhenUseAuthorization()
的对话框,并且是否确实已授予该对话框?你的手机还必须打开蓝牙和位置。是的,我在查找iBeacon时没有遇到任何问题,我只是无法捕捉到它找不到iBeacon的情况。我很惊讶,原来didRangeBeacons是我一直在寻找的方法,尽管它返回一个空数组,我可以使用大小来确定是否找到任何信标。不管怎样,谢谢你的帮助!六羟甲基三聚氰胺六甲醚。。。。好像在那里不流行。我在前面发布的
.start
功能上附加了一个按钮,每次按下按钮几秒钟后都不会发生任何事情。您能否补充您的问题,以显示您在哪里设置LocationManager和委派的完整代码?您是否已申请位置许可?你能展示一下代码吗?
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion)