2017-06-14 111 views
3

我的問題很簡單。我想顯示一條錯誤信息,即「無法找到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)") 
} 

謝謝!

+0

按你的代碼FUNC的LocationManager(_經理:CLLocationManager,monitoringDidFailFor區域:CLRegion?withError錯誤:錯誤){ 打印( 「無法監控區域:\(error.localizedDescription)」) }是正確的檢測失敗在信標監視器失敗 – vivek

+0

您可以使用UIAlertController來顯示消息。確保顯示UIAlertController的調用位於UI線程(主線程)中:https://stackoverflow.com/documentation/ios/874/uialertcontroller#t=201706140716504655358 –

+0

您是否曾經看到過'requestWhenInUseAuthorization() ',你確定它被授予了嗎?您還必須打開藍牙功能併爲您的手機打開位置。 – davidgyoung

回答

0

有趣的是,在委託方法

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

didRangeBeacons被調用與[CLBeacon] S,其中我可以使用beacons陣列尺寸,以確定是否找到任何信標空數組。

不是我所期望的,但是這已解決了我的問題!

2

如果你只是想知道什麼時候沒有檢測到信標(與當時有一個低級別的故障查找信標),則只需使用下面的委託方法:

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

嗯......它似乎並沒有趕上那裏。我在之前發佈的'.start'函數中附加了一個按鈕,幾秒鐘後每次按下時都不會發生任何事情 –

+0

您是否可以增加您的問題以在設置LocationManager和委託的位置顯示完整的代碼?你是否要求位置許可?你可以顯示該代碼嗎? – davidgyoung

+0

剛剛更新了問題。 –