Location Updates changes in iOS 16.4

Location Updates changes in iOS 16.4

First we need to know about CoreLocation Framework before getting into location updates. CoreLocation is the framework that contains CLLocationManager which is the helper class for retrieving location information and handling all events triggered by location changes, e.g. geofence (when user enters or exits a specific CLCircularRegion) and monitoring significant location changes.

To get user location data, we need to use the Core Location Manager, which allows us to request permission to the user's current location, and receive updates about that location. The didUpdateLocations(locations) method, which is a part of CLLocationManager, gives us the real-time user location updates.

For location update's we need to follow few important steps :-

Request Authorization permission to use your user's location.

Add Location Always and When In Use Usage Description and Location Always Usage Description in your info.plist

To get location update's in background then go to Capibilities, open BackgroundModes and click Location updates. That will allow user to get location update's in background mode also.

To start location services first import CoreLocation framework in your ViewController this will provide all the location services.

import CoreLocation

After that add locationManager in your file. locationManager is an object of CLLocationManager class used for retrieving location information and handling all events triggered by location changes.

let locationManager = CLLocationManager()

we need to assign CLLocationManagerDelegate to become delegate of LocationManager, once LocationManager get gps location data, it will know who to report or pass that data to.

locationManager.delegate = self

To start getting location data you just need to call startUpdatingLocation() to starts the generation of updates that report the user’s current location. you can also set the DistanceFilter and DesiredAccuracy according to your need.

locationManager.startUpdatingLocation()locationManager.distanceFilter = nonelocationManager.desiredAccuracy = kCLLocationAccuracyBest

It will send Location events to delegate and ViewController will receive these location data via delegate methods. It will call didUpdateLocations callback method when a new location is tracked.

Also don't forgot to stopUpdatingLocation() to stops the generation of location updates. Continues location update can affect on user battery.

locationManager.stopUpdatingLocation()

If you need only significant location changes than you can call startMonitoringSignificantLocationChanges() to starts the generation of updates based on significant location changes.

locationManager.startMonitoringSignificantLocationChanges()

To stop significant location changes call stopMonitoringSignificantLocationChanges() to stops the delivery of location events based on significant location changes.

locationManager.stopMonitoringSignificantLocationChanges()

For location update in the background continuously you need to simply add,

locationManager.allowsBackgroundLocationUpdates = true
(It indicates whether the app receives location updates when running in the background)

locationManager.pausesLocationUpdatesAutomatically = false
(It indicates whether the location-manager object may pause location updates)

That's it, you will start getting your user's location in foreground and background state of the app. But there is few change's in iOS 16.4 related to location services in background mode.

In iOS 16.4 (and above) there are some changes which causes issue in background location updates and affect the behaviour of apps tracking location in the background.

After iOS 16.4, apps calling both startUpdatingLocation() and startMonitoringSignificantLocationChanges() may get suspended in the background if they are specifying low accuracy and distance filtering in the location manager settings.

If your app needs only low accuracy locations of kCLLocationAccuracyKilometer and up, use startMonitoringSignificantLocationChanges() for that purpose instead.

If your app requires continuous high accuracy locations in the background, you must set the following location manager properties:

  • allowsBackgroundLocationUpdates must be set to TRUE or YES
  • distanceFilter must not be set, or set to kCLDistanceFilterNone
  • desiredAccuracy must be kCLLocationAccuracyHundredMeters or better. If you’re using numeric values, it must be set to less than 1000 meters

Alternatively you can turn on the location indicator which will avoid the issue. You can do this by setting

  • showsBackgroundLocationIndicator to TRUE or YES

If you implement the above changes in your app, its location update behaviour will be similar to behaviour prior to iOS 16.4. Without these changes, your app may not be able to receive continuous background location updates.