iOS Tutorial Background location tracking

Erel

Administrator
Staff member
Licensed User
iLocation library allows you to track the device location when your app is in the foreground.

Using the code posted in this tutorial you can mark your app as a special kind of app that requires background location updates and then your app will continue to run in the background and receive location updates until you call LocationManager.Stop or until the user kills the app.

Apple's documentation: https://developer.apple.com/library...n.html#//apple_ref/doc/uid/TP40009497-CH2-SW3

The first step is to mark the app for background execution:
B4X:
#PlistExtra: <key>UIBackgroundModes</key><array><string>location</string></array>
We also need to describe the reason for the location usage:
B4X:
#PlistExtra:<key>NSLocationAlwaysUsageDescription</key><string>Track your location in the background for better ad revenue.</string>
#PlistExtra:<key>NSLocationUsageDescription</key><string>Used to display the current navigation data.</string>
On iOS 7 the NSLocationUsageDescription string will be used. On iOS 8+ the NSLocationAlwaysUsageDescription string will be used. This replaces the standard NSLocationWhenInUseUsageDescription string. Make sure to update the strings as needed.

Two additional changes:
1. You need to call StartBackground sub instead of Location.Start
2. (optional) Call AllowPauseLocationAutomatically sub and set the activity sub. The OS will use this information to pause the location updates when the location is not expected to change.
You can read more about the activities types: https://developer.apple.com/library.../index.html#//apple_ref/c/tdef/CLActivityType

Edit: It is recommended to disable the AllowPauseLocationAutomatically as explained in post #10.

See the code in the attached project.
Note that the debugger will eventually disconnect when the app is in the background.

Edit: September 2018 - Added new required usage key:
B4X:
#PlistExtra:<key>NSLocationAlwaysAndWhenInUseUsageDescription</key><string>Track your location in the background for better ad revenue.</string>
 

Attachments

Last edited:

Shay

Well-Known Member
Licensed User
Great Thanks Erel !!!!!
Do I need to write different code for IOS 7 and IOS 8? (I did not understand this part)
(if so how to ID between 7 or 8?)
 

Klein

Member
Licensed User
Great!!

On a ios7 device I could all the night long send position data to a server. On ios8 the transfer stops after 15 minutes.

Log says:
Can't endBackgroundTask: no background task exists with identifier 13e45c, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
AllowPauseLocationAutomatically is not used.
 

Klein

Member
Licensed User
Hello,

after 15 minutes the file is no longer updated (ios8) and the location icon is no longer displayed in the status bar!

What am I doing wrong with ios8? I need another background service? Fetch?
 

Erel

Administrator
Staff member
Licensed User
I've tested it after disabling the AllowPauseLocationAutomatically property with this code:
B4X:
Dim no As NativeObject = locManager
no = no.GetField("manager")
no.SetField("pausesLocationUpdatesAutomatically", False)
It worked more than 30 minutes when the device is with its screen closed (until I killed the app).
 

Shay

Well-Known Member
Licensed User
I assume this is how I stop it
B4X:
Sub StopBackground(lm As LocationManager)
    Dim no As NativeObject = lm
    no = no.GetField("manager")
    no.RunMethod("stopUpdatingLocation", Null)
End Sub
 

Shay

Well-Known Member
Licensed User
so now I am confused, since you are starting it with:
no.RunMethod("startUpdatingLocation", Null)
and not with lm.start
 

Turbo3

Active Member
Licensed User
I get two errors when I try to load the example code.

1. "This file is from a newer version and it may run improperly. Please check www.basic4ppc.com for updates"
2. "Error accessing the following files. ... 1.bil"

I am running B4i version 1.80 is that not the latest version? Where do we find the version number for the latest version on this website? The zip has no 1.bil file that I can see.
 

Turbo3

Active Member
Licensed User
Thanks for the pointer to version number and updated example. Now the example code loads fine.

The new version skips this code to set the "activeType". Why?
B4X:
no.SetField("activityType", ActivityType)
 

Turbo3

Active Member
Licensed User
Yes, I understood that but what was the purpose of the "no.SetField("activityType", ActivityType)" line that is no longer called. It looks like it was previously used to tell the operating system the type of activity was "2 - Automotive Navigation".
 
Top