-->

Use of Standard location service for tracking trav

2020-07-24 03:06发布

问题:

I have to develop a kind of GPS navigation application that needs to constantly keep track of the position of the user, which is moving by car. In the specific, I don't have to display the current location on a map but just to record its position with the best possible precision in order to calculate the total distance traveled.

Of course the application needs to continue its work in background if the user switch to another app, a phone call come in or something like that…

From the tests I have made and from what I have learned from this useful post (and from the documentation), it seems to me that the best choice in this case is the Standard location service, with the application configured with the UIBackgroundModes = location in the .plist file. In this way it will continue to receive the location updates even if in background and it will never be suspended by the OS (this is actually true only if [locationManager startUpdatingLocation] has been called). This is also confirmed by this guys. I have personally verified that is true simply by running the app with Instruments and the Memory monitor module where you can see the various flags about the app states, putting the app in background first with UIBackgroundModes set to location and startUpdatingLocation active, and after without it.

  • 1) I'm now wondering what to do when the app is terminated when it is tracking the position. I don't want to loose any location updates so I need to wake up it again whenever a new update is available. The documentation say:

Important: The applicationWillTerminate: method is not called if your app is currently suspended.

But because in my case it will be never suspended (it will stay in background but not suspended), my logical conclusion is that applicationWillTerminate will be always called and so I could register for a Significant location update or Region monitoring inside of this method in order to be waked up and then restart the Standard location service. Is applicationWillTerminate the right place to put this code?

  • 2) An application working in background but not suspended could be terminated by the OS for no other reasons than a very low memory condition or for my app don't properly respond to a memory warning? (the user could also manually close it). I was concerned if applicationWillTerminate wouldn't be called in some way.

  • 3) Could Apple not approve an app which constantly use the standard location services in background because of its quick battery drain? Have you ever had approve problems for similar apps?

回答1:

since you are asking many questions which you shoukd not do here, i cam give you only aswrs to a part of it.

if the app is terminated, then you cannot restart it anymore. This is usually the case when the user terminates the app.
Dont worry Apple ( ios) will not terminate your app. your app will not use much memory, games with huge bitmap graphics are more likely to be termin.

  1. evry gps app will drain the bat. that is not a reason for not aproving.

  2. suspended means that app is not in background mode, it is sleeping. you will not receive Gps, so there is no need to call you on terminate. you have to save data before, see the apple docu on background modes and app life cycle.