【发布时间】: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