Swift Geofencing works with a GPX file, but not on a real device

when I run the code below using my phone connected to my mac I have a GPX file and in the debugger I select "Locations", everything works fine, every region that I have in the GPX file starts up, a notification appears without problems .

The problem is when I take my phone to drive, and I transmit exactly the regions that I have in my GPX file, notifications do not start only from the first.

By method

func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
        print("Hi \(region.identifier)")

}

I see that all regions have been started for monitoring

I’ve been stuck with this for several days ... Could you help someone help me.

Below is the code.

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)
                }
            })
        }
    }
+4

Source: https://habr.com/ru/post/1657574/


All Articles