when I run the code below testing using my phone connected to my mac I have a GPX file and on the debugger I select "Locations" everything works fine, every region I have on the GPX file gets triggered, the notification comes up with no problem.
The issue is when I take my phone for a car ride and I pass the exactly regions I have on my GPX file , the notifications don't get triggered just the first one.
On the method
func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
print("Hi \(region.identifier)")
}
I can see all the regions had been started for monitoring
I've been stuck on this for quite few days now... Could you please someone help me .
Code follows below.
func GetAllILocations(){
if let url = URL(string: "http://www.goemobile.com/mobile/liquidnitro/getlocations.php"){
var request = URLRequest(url:url)
request.httpMethod = "POST";// Compose a query string
let postString = ""
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with:request) { data, response, error in
if error != nil{
return
}
do {
if let convertedJson = try JSONSerialization.jsonObject(with: data!, options: []) as? [[String:Any]] {
for location in convertedJson {
if ((location["locationid"] as? Int)! > 0) {
let latitude = location["latitude"] as! Double
let longitude = location["longitude"] as! Double
let title = location["locationtitle"] as! String
let subtitle = location["locationsubtitle"] as? String
let annotation = MKPointAnnotation()
annotation.title = title
annotation.subtitle = subtitle
annotation.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
self.mapView.addAnnotation(annotation)
let center = CLLocationCoordinate2DMake(latitude, longitude)
let region = CLCircularRegion.init(center: center, radius: 0.5, identifier: title)
region.notifyOnEntry = true;
region.notifyOnExit = false;
self.locationManger.startMonitoring(for: region)
}
}
}
}
catch let error as NSError {
print(error.localizedDescription)
}
}
task.resume()
}
}
func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
print("Hi \(region.identifier)")
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
lblRegion.text = region.identifier
if region is CLCircularRegion {
scheduleNotification(inSeconds: 0.5, storeName:region.identifier, completion: {success in
if !success{
let alert = UIAlertController(title: "Error Alert", message: region.identifier, preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
})
}
}