Couple of things to be changed.
Step 1:
Make sure you have enabled location updates background mode in capabilities section of your project as shown below
Step 2:
And when I'm killing the app location update against stoped.
Quoting from apple docs
If you start this service and your app is subsequently terminated, the
system automatically relaunches the app into the background if a new
event arrives. In such a case, the options dictionary passed to the
application(:willFinishLaunchingWithOptions:) and
application(:didFinishLaunchingWithOptions:) methods of your app
delegate contains the key location to indicate that your app was
launched because of a location event. Upon relaunch, you must still
configure a location manager object and call this method to continue
receiving location events. When you restart location services, the
current event is delivered to your delegate immediately. In addition,
the location property of your location manager object is populated
with the most recent location object even before you start location
services.
link : https://developer.apple.com/documentation/corelocation/cllocationmanager/1423531-startmonitoringsignificantlocati
important thing to notice in above statement is
Upon relaunch, you must still, configure a location manager object and
call this method to continue receiving location events.
Meaning, your current location manager will not be of much use and you should create a new one and configure the new instance and call startMonitorSignificantLocationChanges
again.
So iOS will send location updates to terminated apps only when you use startMonitoringSignificantLocationChanges
.
All that are applicable only if your app was terminated and iOS relaunched it on receiving location update. But if your app is simply in background you need not do any thing on using startMonitorSignificantLocationChanges
on the other hand startUpdatingLocation
will work only when app is in background/foreground mode. iOS will stop updating location if your app gets suspended or killed.
If you start this service and your app is suspended, the system stops
the delivery of events until your app starts running again (either in
the foreground or background). If your app is terminated, the delivery
of new location events stops altogether. Therefore, if your app needs
to receive location events while in the background, it must include
the UIBackgroundModes key (with the location value) in its Info.plist
file.
link: https://developer.apple.com/documentation/corelocation/cllocationmanager/1423750-startupdatinglocation
So modify your code
locationManager.startMonitoringSignificantLocationChange()
Ok that was about proper usage of startMonitoringSignificantLocationChanges
and startupdatinglocation
. Now timer for mistakes in your code.
Mistake 1:
self.bgtimer = Timer.scheduledTimer(timeInterval: 30, target: self, selector: #selector(self.bgtimer(timer:)), userInfo: nil, repeats: true)
Using timer to get timely updates on location. Thats not how it works!!! You cant run Timer forever. Timer stops as soon as your app suspends or gets terminated. Location Manager informs the app when location changes and you should rely on that only. You cant run timer to timely check location updates. It won't run in suspended or terminated state.
Mistake 2:
func updateLocation() {
locationManager.startUpdatingLocation()
locationManager.stopUpdatingLocation()
Why start and stopping update locations in subsequent statements? That does not make much sense.
Mistake 3:
func StartupdateLocation() {
locationManager.delegate = self
locationManager.startUpdatingLocation()
Your StartupdateLocation
gets called multiple time and every time you called this method you are repeatedly calling startUpdatingLocation
on same instance of location manager. You need not do that! You can call startUpdatingLocation
or startMonitoringSignificantLocationChange
only once.