Working with location updates when app is terminated
Asked Answered
T

1

4

I am working on a project in which i am trying to get location updates in all states, even when app is terminated. I have used all possible solutions but still it's not working in case of termination.For now I want to clear a doubt - I am using startUpdatingLocation() for foreground and background. As we know that startMonitoringSignificantLocationChanges() is the only method that relaunch app in case of any location update. Can we call "startMonitoringSignificantLocationChanges()" in applicationWillTerminate() method? and Will that work and relaunch app when there is any significant location update? Please tell me.

Thank!!

Turbid answered 4/1, 2016 at 10:59 Comment(0)
M
2

You cannot do that in applicationWillTerminate(),because closure won't return a value right now.If you want to get user location all the time,try Background Mode.

This is the description in Apple Document:

In such a case, the options dictionary passed to the application:willFinishLaunchingWithOptions: and application:didFinishLaunchingWithOptions: methods of your app delegate contains the key UIApplicationLaunchOptionsLocationKey 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.

It clearly tells you how to get the location.

Murat answered 4/1, 2016 at 11:7 Comment(8)
Using Background mode, we can get location updates in foreground and background only, not ater the app is terminated. right?Turbid
@KKb Background Mode will allow you get location even though your app is inactive.Murat
@Lumialxk...and what if i want updates when app is terminated,suspended or killed?Turbid
and its not clear to me that why it will not work if we call it in 'applicationWillTerminate()' method but it works in DidEnterBackground method?Turbid
@KKb Actually,you won't get any notification when the app was killed by user or system.Even if you get a notification,closure won't return value immediately.To summarize,terminate function always couldn't work.The enter background function is different. Your app will not be killed at the next second,so the closure will be called.Murat
@KKb 'startMonitoringSignificantLocationChanges' function will awake your app when there is a significant change of location (when your app is in background).But the Background Mode is more powerful.It is designed for working all the time app by Apple.So it depends on how often or how accurately you want to get the location.Murat
@Lumialxk..Thanks for your help!!..So what can we do to get loaction updates when app is terminated/suspended. And in apple Doc its written- "f you leave the significant-change location service running and your iOS app is subsequently suspended or terminated, the service automatically wakes up your app when new location data arrives".Turbid
@KKb Updated my answer.Murat

© 2022 - 2024 — McMap. All rights reserved.