Voici une version abrégée de ce que j'essaie de faire avec la surveillance de la région de la balise à l'aide de CLLocationManager.Surveillance Swift 3 Beacon utilisant CLLocationManager
J'ai besoin d'être informé des événements d'entrée/sortie de plusieurs régions (moins de 20).
Je reçois des événements d'entrée d'emplacement cohérents même en arrière-plan. Je ne reçois pas les événements de sortie au premier plan dans la vue Client ou en arrière-plan. À un moment donné, j'ai réussi à obtenir des événements de sortie en arrière-plan, mais je n'obtenais pas un nombre variable de régions de balises à écouter.
Y a-t-il des erreurs de convention/logique dans ce code?
//------------------
/* Client View Controller - Main Profile View */
class ClientVC: UIViewController, ClientVC_Preferences_Protocol, OpenPreferencesProtocol, ClientVCProtocol {
\t /* Reference to AppDelegate file where location manager resides */
\t let appDelegate = UIApplication.shared.delegate as! AppDelegate
\t override func viewDidLoad(){
\t \t // .. Obtain some beacon info - not shown
\t \t for beacon in beacons {
\t /* Create a region for each beacon and start monitoring */
\t var region = CLBeaconRegion(proximityUUID: UUID(uuidString:beacon.UUID)!, identifier: beacon.Identifier)
\t region.notifyEntryStateOnDisplay = true
\t region.notifyOnExit = true
\t region.notifyOnEntry = true
\t self.appDelegate.locationManager.startMonitoring(for: region)
\t }
\t }
\t /* Protocol function to alert client when exit event occured */
\t func alertClient(businessName:String) {
let notification = UILocalNotification()
notification.alertBody = "Did you leave " + businessName + "?"
UIApplication.shared.presentLocalNotificationNow(notification)
let alertController = UIAlertController(title: businessName, message: "Did you leave?", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Yes, I Left!", style: UIAlertActionStyle.default) {
UIAlertAction in
\t // .. Do some firebase work - not shown
}
let cancelAction = UIAlertAction(title: "No, I'm Here!", style: UIAlertActionStyle.cancel) {
UIAlertAction in
// .. Do some firebase work - not shown
}
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.present(alertController, animated:true, completion:nil)
}
}
//------------------
/* AppDelegate */
/* Protocol connected to Client View Controller */
protocol ClientVCProtocol{
func alertClient(businessName:String) /* Displays alert to client when exiting the region */
func reloadTableView() /* Refreshes table view */
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
\t /* Delegate for ClientVC Protocol - used to call methods */
\t var ClientVCDelegate:ClientVCProtocol?
\t /* Instantiate location manager */
\t var locationManager = CLLocationManager()
\t /* Triggered by entering a region */
\t func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
print("Enter event " + region.identifier)
// .. Do some firebase work - not shown
\t }
\t /* Triggered by exiting a region */
\t func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
\t \t /* Break set on this line to ensure whether or not this function is called */
\t \t print("Exit Attempt")
\t \t /* Gets business name associated with region and calls protocol function to ClientVC */
\t \t if let beaconRegion = region as? CLBeaconRegion {
self.getBusinessName(region: region){
(result: String) in
print("Exit event " + region.identifier)
self.ClientVCDelegate?.alertClient(businessName:result)
}
}
\t }
\t /* Runs when application finishes launching - configures location manager */
\t func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.fbRef = Database.database().reference()
UIApplication.shared.registerUserNotificationSettings(
UIUserNotificationSettings(types: .alert, categories: nil))
/* Not sure how relevent all of these calls are */
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = 2000
if #available(iOS 9.0, *) {
locationManager.allowsBackgroundLocationUpdates = true
}
locationManager.startMonitoringSignificantLocationChanges()
locationManager.pausesLocationUpdatesAutomatically = true
locationManager.requestAlwaysAuthorization()
return true
}
}
Veuillez excuser une partie de l'indentation de code, je ne pouvais pas l'obtenir de reporter à l'identique. –