I have been trying to find a solution but no luck :/ I want to call my firebase data strings and use them as the "title" and "message" in a UIAlertView. I am using this UIAlertView as a geofence message. The geofence works if I just set the UIAlertView to a basic one where I enter in the message, yet I need it to call the message they wrote for another user to read. So far this setup only pops up the "Ok" button and nothing else once entered into a region.
func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
showAlert(withTitle: name, message: message)
//showAlert(withTitle: "Enter \(region.identifier)", message: "Geofence Message")
print(state)
print("region :\(region.identifier)")
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
showAlert()
//showAlert(withTitle: "Enter \(region.identifier)", message: "Geofence Message")
print("DID ENTER REGION")
}
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
//showAlert(withTitle: "Exit \(region.identifier)", message: "Message Exit")
//TODO: stop local sequence
print("DID EXIT REGION")
}
func showAlert(withTitle title: String?, message: String?) {
FIRDatabase.database().reference().child("Businesses").observeSingleEvent(of: .value, with: { snapshot in
if let dictionary = snapshot.value as? [String: AnyObject] {
self.name = dictionary["businessName"] as? String
self.message = dictionary["geofenceMessage"] as? String
}
let alert = UIAlertController(title: self.name, message: self.message, preferredStyle: .alert)
let action = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
})
}
More Info
// Populate Map With Firebase Businesses
func loadPlaces(){
if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) {
FIRDatabase.database().reference().child("Businesses").observe(.value, with: { snapshot in
self.locationData = snapshot.value as? NSDictionary
if let data = self.locationData{
for (key,obj) in data{
let value = obj as? NSDictionary
let locationValue = value as! [String: Any]
let lat = Double(locationValue["businessLatitude"] as! String)
let long = Double(locationValue["businessLongitude"] as! String)
let businessTitle = String(locationValue["businessName"] as! String)
let center = CLLocationCoordinate2D(latitude: lat!, longitude: long!)
let radius = CLLocationDistance(500.0)
let geoRegion = CLCircularRegion(center: center, radius: radius, identifier: businessTitle!)
self.geofences.append(geoRegion)
self.locationManager.startMonitoring(for: geoRegion)
let overlay = MKCircle(center: center, radius: radius)
self.mapView.add(overlay)
geoRegion.notifyOnEntry = true
geoRegion.notifyOnExit = true
let annotation = MKPointAnnotation()
annotation.coordinate = geoRegion.center
annotation.title = businessTitle
self.mapView.addAnnotation(annotation)
self.nameKeyDict[(value?.value(forKey: "businessName") as? String)!] = key as? String
}
}
})
} else {
print("No Bueno")
}
}
Firebase Data Structure