iOS Tutorial Background location tracking

Discussion in 'iOS Tutorials' started by Erel, Feb 4, 2015.

  1. Erel

    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:
    Code:
    #PlistExtra: <key>UIBackgroundModes</key><array><string>location</string></array>
    We also need to describe the reason for the location usage:
    Code:
    #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.
     

    Attached Files:

    Last edited: Feb 24, 2015
    Mark Turney likes this.
  2. Shay

    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?)
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    You don't need to write different code.
    There are two description strings. One for NSLocationAlwaysUsageDescription and one for NSLocationUsageDescription.
    iOS 8+ uses the NSLocationAlwaysUsageDescription string.
     
    naifnas likes this.
  4. Klein

    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:
    AllowPauseLocationAutomatically is not used.
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    The device has probably turned off its wireless feature. Try to save the data to a file and see whether it continues to update the file.
     
  6. Klein

    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?
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Can you upload your project (File - Export as zip)?
     
  8. Klein

    Klein Member Licensed User

    can i send a mail?
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    Yes (please try to recreate it in a project with only this feature). Send it to erel@basic4ppc.com.
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    I've tested it after disabling the AllowPauseLocationAutomatically property with this code:
    Code:
    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).
     
  11. Klein

    Klein Member Licensed User

    well it works :)
     
  12. Shay

    Shay Well-Known Member Licensed User

    I assume this is how I stop it
    Code:
    Sub StopBackground(lm As LocationManager)
        
    Dim no As NativeObject = lm
        no = no.GetField(
    "manager")
        no.RunMethod(
    "stopUpdatingLocation"Null)
    End Sub
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    No. You should call lm.Stop.
     
  14. Shay

    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
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    The reason it is started with NativeObject is that you need to call requestAlwaysAuthorization. The stop method is still the same.
     
  16. Turbo3

    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.
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    The latest version is 1.80. You can see it here (bottom right corner): http://www.b4x.com/

    I've uploaded a new zip file that fixes these issues.
     
  18. Turbo3

    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?
    Code:
    no.SetField("activityType", ActivityType)
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    See the bold "edit" sentence in the tutorial.

    After several tests we realized that in order to keep the app running without limits the AllowPauseLocationAutomatically property should be disabled.
     
  20. Turbo3

    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".
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice