Ma question est très simple. Je voudrais afficher un message d'erreur à-dire « Vous ne trouvez pas IBeacon » si la surveillance IBeacon échoue, après avoir appelé startSearchingForSessions
par une pression sur un bouton après avoir été appelé à viewDidLoad
Affiche le message "Can not find 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()
}
et la manipulation des balises trouvées ainsi:
// 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()
}
J'ai mis en œuvre les méthodes d'erreur de délégué dans une tentative de trouver où cela se produit, mais jusqu'à présent, en naviguant sur les monticules de la documentation sur iBeacon, je suis arrivé infructueux.
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)")
}
Merci!
selon votre code func LocationManager (_ gestionnaire: CLLocationManager, région monitoringDidFailFor: CLRegion ?, erreur withError: Error) { print ("région de surveillance a échoué: \ (error.localizedDescription)") } est juste de détecter échoue in beacon monitor fail – vivek
Vous pouvez utiliser UIAlertController pour afficher le message. Assurez-vous que l'appel pour afficher le UIAlertController est dans le thread UI (thread principal): https://stackoverflow.com/documentation/ios/874/uialertcontroller#t=201706140716504655358 –
Avez-vous déjà vu la boîte de dialogue pour requestWhenInUseAuthorization() ', et êtes-vous sûr que c'est accordé? Vous devez également avoir allumé Bluetooth et l'emplacement allumé pour votre téléphone. – davidgyoung