2017-06-14 1 views
3

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é à viewDidLoadAffiche 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!

+0

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

+0

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 –

+0

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

Répondre

0

Il est intéressant, didRangeBeacons dans le procédé de délégué

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

est appelée avec un réseau de vide [CLBeacon] s, dans lequel je peux utiliser la taille du tableau beacons pour déterminer si oui ou non toutes les balises ont été trouvés.

Pas ce que j'attendais, mais cela a résolu mon problème!

2

Si vous voulez simplement savoir quand les balises ne sont pas détectés (contre quand il y avait un échec de bas niveau pour rechercher des balises), puis il suffit d'utiliser la méthode déléguée suivante:

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

hmmm .... il semble que ce n'est pas attraper là. J'ai un bouton attaché aux fonctions '.start' affichées plus tôt, et sur chaque presse après quelques secondes rien ne se passe –

+0

Pouvez-vous augmenter votre question pour montrer votre code complet où vous configurez votre LocationManager et déléguez? Avez-vous demandé l'autorisation de localisation? Pouvez-vous montrer ce code? – davidgyoung

+0

juste mis à jour la question. –