I have this code that works fine on fore and background but the didExitRegion event will not call when I force killed the app. The springboard shows the geofencing location icon but nothing will come in. Am I missing something?
Any comments are greatly appreciated.
I have the appropriate background mode "
App registers for location updates
& "Privacy - Location Always Usage Description
in info.plist.let regionMgr = CLLocationManager()
var didUpdateLocation = false
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { / setAppAppearances() checkNetworkReachability() / if CLLocationManager.authorizationStatus() == .authorizedAlways { setlocationSettings() } } return true }
func setlocationSettings() { regionMgr.requestAlwaysAuthorization() regionMgr.stopMonitoringAllRegions() regionMgr.allowsBackgroundLocationUpdates = true regionMgr.desiredAccuracy = kCLLocationAccuracyHundredMeters regionMgr.activityType = CLActivityType.fitness regionMgr.delegate = self regionMgr.requestLocation() }
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { kLastLocation = locations.last! let lat = String(describing: kLastLocation.coordinate.latitude) let long = String(describing: kLastLocation.coordinate.longitude) if didUpdateLocation != true { NSLog("didUpdateLocations appDelegate") DispatchQueue.main.asyncAfter(deadline: delayTime(1.0)) { sendTrajectories(long, lat) } didUpdateLocation = true setlocationSettings() / self.createRegion(kLastLocation) } else { NSLog("updated location again and again lol"); regionMgr.stopUpdatingLocation() } }
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { print("error:: \(error.localizedDescription)") }
func createRegion(_ loc:CLLocation?) { if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) { let coordinate = CLLocationCoordinate2DMake((loc?.coordinate.latitude)!, (loc?.coordinate.longitude)!) let regionRadius = 100.0 / let region = CLCircularRegion(center: CLLocationCoordinate2D( latitude: coordinate.latitude, longitude: coordinate.longitude), radius: regionRadius, identifier: "region") region.notifyOnExit = true region.notifyOnEntry = true / NSLog("createRegion called") / if CLLocationManager.authorizationStatus() == .authorizedAlways { setlocationSettings() regionMgr.distanceFilter = 100 regionMgr.startMonitoring(for: region) NSLog("startMonitoring \(region)") } } else { print("system can't track regions") } }
func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) { NSLog("didStartMonitoringForRegion"); regionMgr.requestStateForRegion(region); }
func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) { NSLog("Region monitoring failed with error: \(error.localizedDescription)") }
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) { NSLog("Entered Region") }
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) { NSLog("Exited Region") / / setlocationSettings() didUpdateLocation = false / regionMgr.requestLocation() regionMgr.startUpdatingLocation() }