I'm working on the app this has ability to monitoring user location even if app is suspended.
I'm experiment with geofencing feature of Core Location.
According to docs: https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html
But in reality I see that when app was woken up by this launch option - it's never (or at least very long time) get terminated.
My code is this:
So I've added region monitoring for my current location, killed the app, and go for a walk.
As I expected after I exit the region - I got the notification App awake, then Exit the region. Remove this region. Requesting current location and after it - Added new geofencing by current location: {my location}.
But what I not expect - is that I never get the App awake notification again after 30 min of walking. Every ~ 2 minutes I've got:
Exit the region. Remove this region. Requesting current location
Added new geofencing by current location: {my location}.
So this means that app never get killed in background after it was launched initially (after first location event).
Why is that? Is I misunderstood something? Where I can read this in docs?
I'm experiment with geofencing feature of Core Location.
According to docs: https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html
It's said about Significant-change location service but I assumed that if they both (Region monitoring and Significant-change location) wake up app by UIApplicationLaunchOptionsKey.location key - behaviour must be similar.At wake-up time, the app is put into the background and you are given a small amount of time (around 10 seconds) to manually restart location services and process the location data. (You must manually restart location services in the background before any pending location updates can be delivered
But in reality I see that when app was woken up by this launch option - it's never (or at least very long time) get terminated.
My code is this:
Code Block import CoreLocation import UserNotifications @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var locationManagerForRegionMonitoring: CLLocationManager? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { if let launchOpt = launchOptions { if launchOpt[.location] != nil { showLocalNotification(with: "App awake") locationManagerForRegionMonitoring = CLLocationManager() locationManagerForRegionMonitoring!.delegate = self locationManagerForRegionMonitoring!.requestAlwaysAuthorization() locationManagerForRegionMonitoring!.pausesLocationUpdatesAutomatically = true } } registerNotifications() return true } func addNewRegion(with coord: CLLocationCoordinate2D) { let region = CLCircularRegion(center: coord, radius: 50, identifier: UUID().uuidString) region.notifyOnEntry = false region.notifyOnExit = true locationManagerForRegionMonitoring?.startMonitoring(for: region) } } extension AppDelegate: CLLocationManagerDelegate { func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { guard let location = locations.last else { return } showLocalNotification(with: "Added new geofencing by current location: \(location)") addNewRegion(with: location.coordinate) } func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { showLocalNotification(with: "\(error.localizedDescription)") } func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) { guard region is CLCircularRegion else { return } let msg = "Exit the region. Remove this region. Requesting current location" showLocalNotification(with: msg) locationManagerForRegionMonitoring?.stopMonitoring(for: region) locationManagerForRegionMonitoring?.requestLocation() } }
So I've added region monitoring for my current location, killed the app, and go for a walk.
As I expected after I exit the region - I got the notification App awake, then Exit the region. Remove this region. Requesting current location and after it - Added new geofencing by current location: {my location}.
But what I not expect - is that I never get the App awake notification again after 30 min of walking. Every ~ 2 minutes I've got:
Exit the region. Remove this region. Requesting current location
Added new geofencing by current location: {my location}.
So this means that app never get killed in background after it was launched initially (after first location event).
Why is that? Is I misunderstood something? Where I can read this in docs?