Background User Location When App Is Terminated/Suspended

Does the location background mode work in swift when the app is terminated?

When you terminate the app, the normal* location services stop, until you run startUpdatingLocation from a non-background state.


*visit Location Service, region monitoring, significant Location Changes are not normal location tracking and are managed on the OS level not app level.

Non-normal location tracking won't get stopped after app termination.

Update Location when app is terminated

In a word, no. You could subscribe to significant location change notifications. Then when the system detects that the user has moved by "a significant distance" (defined by the system.) I'm not sure what the system calls a significant location change, but it's going to be a lot more than 50 feet.

Another alternative would be to create a geofence around the user's current location and listen for location updates using the Core Location startMonitoring(for:) function.

Both of those techniques will relaunch your app if they detect a relevant event and your app isn't running.

iOS swift backgound location update when killed/suspended with minimum data loss

question-1

What is the difference between using startMonitoringSignificantLocationChanges Vs startUpdatingLocation?

startUpdatingLocation updates the location when it is called first time and then when the distance filter value exceeds.

it uses the GPS when its available, if you use in continuously it Drain your power/battery

Discussion

This method returns immediately. Calling this method causes the
location manager to obtain an initial location fix (which may take
several seconds) and notify your delegate by calling its
locationManager:didUpdateLocations: method. After that, the receiver generates update events primarily when the value in the distanceFilter property is exceeded. Updates may be delivered in other situations though. For example, the receiver may send another notification if the hardware gathers a more accurate location reading.

Calling this method several times in succession does not automatically
result in new events being generated. Calling stopUpdatingLocation in
between, however, does cause a new initial event to be sent the next
time you call this method.

If you start this service and your application is suspended, the
system stops the delivery of events until your application starts
running again (either in the foreground or background). If your
application is terminated, the delivery of new location events stops
altogether. Therefore, if your application needs to receive location
events while in the background, it must include the UIBackgroundModes
key (with the location value) in its Info.plist file.

In addition to your delegate object implementing the
locationManager:didUpdateLocations: method, it should also implement
the locationManager:didFailWithError: method to respond to potential
errors.

startMonitoringSignificantLocationChanges when a significant change in position occurs.

it uses cellular or wifi, when it work the location is changed or its called in the particular Time interval.

Discussion

This method initiates the delivery of location events asynchronously,
returning shortly after you call it. Location events are delivered to
your delegate’s locationManager:didUpdateLocations: method. The first
event to be delivered is usually the most recently cached location
event (if any) but may be a newer event in some circumstances.

Obtaining a current location fix may take several additional seconds,
so be sure to check the timestamps on the location events in your
delegate method.

After returning a current location fix, the receiver generates update
events only when a significant change in the user’s location is
detected. For example, it might generate a new event when the device
becomes associated with a different cell tower. It does not rely on
the value in the distanceFilter property to generate events. Calling
this method several times in succession does not automatically result
in new events being generated. Calling stopMonitoringSignificantLocationChanges in between, however, does
cause a new initial event to be sent the next time you call this
method.

If you start this service and your application is subsequently
terminated, the system automatically relaunches the application into
the background if a new event arrives. In such a case, the options
dictionary passed to the locationManager:didUpdateLocations: method of
your application delegate contains the key
UIApplicationLaunchOptionsLocationKey to indicate that your
application 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.

Note:

Apps can expect a notification as soon as the device moves 500
meters or more from its previous notification. It should not expect
notifications more frequently than once every five minutes. If the
device is able to retrieve data from the network, the location manager
is much more likely to deliver notifications in a timely manner.

for differenciate purpose I taken from here

question-2

If we use startUpdatingLocation does it impact on publishing the app to the App Store?

One of the possible reasons for 2.16 rejection is the absence of GPS battery warning in your app description on the app meta in iTunesConnect - "The continued use of GPS may decrease battery life" or something like that.

for More information related to this

Question-3

When the app is killed/suspended (Force closed by the user) it takes some time to restart the location manager from the AppDelegate which causes loss of location data for a period of time. Any possible solution to overcome this

NO, we Can't Over Come, reason the memory newly Initiated.

How to get and send location(even if application is terminated or suspended) to server

According to the apple in the document about the description of the background, any app background task execution time of 10 minutes.After 10 minutes, the app will be iOS forced to hang up.
But there are five types of app allows for "unlimited" background running time.

  1. Audio。

  2. Location/GPS。

  3. VoIP。

  4. Newsstand。

  5. Exernal Accessory 。

You can take any app statement for the above five types in order to obtain the background of infinite time, but when you submit the app to the app Store, apple will review your app, once found you "abuse" of the backend API, your app will be rejected.
However, in the case of enterprise development, there is no "abuse" problem - enterprise app can through OTA deployment, not through the apple store review.
In enterprise deployment, you can be a statement for VoIP app, but the program simply has nothing to do with VoIP, we only for the purpose of the iOS give us infinite background execute permissions.Declaration process is in the app Info. The file to add the following key:
In the first place in the file the Required background modes that add the following two: in a App play audio or streams audio/video using AirPlay and App provides Voice over IP services.
UIBackgroundModes

voip

I tested the following code:

- (void)backgroundHandler {
NSLog(@"### -->backgroundinghandler");
UIApplication* app = [UIApplicationsharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
}

Through the test, I have received an "infinite" background execution time.I don't know how long do you think is "infinite", but in this case, the background task to run at least 55 hours, until I lose patience to stop testing.
my english is very poor,this blogs are reprinted!



Related Topics



Leave a reply



Submit