iOS Question iOS Location Tracking in Background

walterf25

Expert
Licensed User
Longtime User
Hi All, i have the need to create an app which tracks location in the background, i followed Erel's example here https://www.b4x.com/android/forum/threads/background-location-tracking.50246/.

The example compiles and runs just fine, but i don't see the location updating at all, in fact i don't see the dialog that requests for permission to allow the location tracking, If I run his other example here https://www.b4x.com/android/forum/threads/location-gps.46148/#content, then i can see the location being updated just fine and I see the dialog which requests permission.

One thing I did notice is that in the logs I see the following message, i'm not sure if this is the cause of the example not providing the location updates?
This app has attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain both NSLocationAlwaysAndWhenInUseUsageDescription and NSLocationWhenInUseUsageDescription keys with string values explaining to the user how the app uses this data

I'am using iPhone 8 plus running iOS 11.4.1 on it and i'am using the remote compiler to run this examples.

My code is the following:
B4X:
'Code module
#Region  Project Attributes
    #ApplicationLabel: B4i Example
    #Version: 1.0.0
    #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
    #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
#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>
#PlistExtra: <key>UIBackgroundModes</key><array><string>location</string></array>
''#PlistExtra:<key>NSLocationWhenInUseUsageDescription</key><string>Used to display the current navigation data.</string>
''#PlistExtra:<key>NSLocationUsageDescription</key><string>Used to display the current navigation data.</string>
#End Region


Sub Process_Globals
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private locManager As LocationManager
    Private lblCount As Label
    Private counter As Int = 0
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.RootPanel.LoadLayout("1")
    NavControl.ShowPage(Page1)
    locManager.Initialize("locManager")
    Dim no As NativeObject = locManager
    no = no.GetField("manager")
    no.SetField("pausesLocationUpdatesAutomatically", False)
    StartBackground(locManager, 0) 'replaces locManager.Start
End Sub

Sub UpdateLabel
    lblCount.Text = "Number of updates: " & counter
End Sub

Sub StartBackground(lm As LocationManager, MinimumDistance As Double)
    Dim no As NativeObject = lm
    no = no.GetField("manager")
    If App.OSVersion >= 8 Then
        no.RunMethod("requestAlwaysAuthorization", Null)
    End If
    If App.OSVersion >= 9 Then
        no.RunMethod("setAllowsBackgroundLocationUpdates:", Array(True))
    End If
    no.SetField("distanceFilter", MinimumDistance)
    no.RunMethod("startUpdatingLocation", Null)
End Sub

'ActivityType:
'1 - Other
'2 - Automotive Navigation
'3 - Fitness
'4 - Other navigation
Sub AllowPauseLocationAutomatically(lm As LocationManager, ActivityType As Int) 'ignore
    Dim no As NativeObject = lm
    no = no.GetField("manager")
    no.SetField("activityType", ActivityType)
    no.SetField("pausesLocationUpdatesAutomatically", True)
End Sub

Sub LocManager_LocationChanged (Location1 As Location)
    Log(Location1.Latitude & ", " & Location1.Longitude)
    counter = counter + 1
    UpdateLabel
End Sub

Sub LocManager_LocationError
    Log("error: " & LastException.Message)
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)
  
End Sub

Private Sub Application_Background
  
End Sub

Any thoughts or advice on what could be going on, I tried running the example in both debug and release mode and i see the same results.

Thanks in advance for any help.

Walter
 
Top