我的問題很簡單。我想顯示一條錯誤信息,即「無法找到iBeacon顯示」如果iBeacon顯示監控失敗,被稱爲viewDidLoad
顯示「無法找到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()
}
後通過按下一個按鈕調用startSearchingForSessions
正是如此處理髮現信標之後:
// 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(_經理:CLLocationManager,monitoringDidFailFor區域:CLRegion?withError錯誤:錯誤){ 打印( 「無法監控區域:\(error.localizedDescription)」) }是正確的檢測失敗在信標監視器失敗 – vivek
您可以使用UIAlertController來顯示消息。確保顯示UIAlertController的調用位於UI線程(主線程)中:https://stackoverflow.com/documentation/ios/874/uialertcontroller#t=201706140716504655358 –
您是否曾經看到過'requestWhenInUseAuthorization() ',你確定它被授予了嗎?您還必須打開藍牙功能併爲您的手機打開位置。 – davidgyoung