iOS Tutorial Background location tracking

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

  1. fbritop

    fbritop Active Member Licensed User

    Thanks Erel,
    The problem was when we use multiple instances of UIBackgroundModes:

    Code:
    '#PlistExtra: <key>UIBackgroundModes</key><array><string>location</string></array>
      
        
    #PlistExtra:<key>UIBackgroundModes</key><array><string>location</string><string>remote-notification</string><string>bluetooth-central</string></array>
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Does it now work properly?
     
  3. fbritop

    fbritop Active Member Licensed User

    Yes now it works
     
  4. franchsesko

    franchsesko Member Licensed User

    Hi,

    I'm try to get the OBJC part to work but I get a runtime error when my StartBackground() method is called.
    Would you have any idea why ?

    This is the error
    Code:
    Application_Start
    Error occurred on line: 
    67 (Main)
    Method 
    not found: isSignificantLocationAvailable, target: <B4ILocationManager: 0x156552b0>
    Stack Trace: (
      CoreFoundation       <redacted> + 
    150
      libobjc.A.dylib      objc_exception_throw + 
    38
      CoreFoundation       <redacted> + 
    0
      LocationManagerTest  +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 
    420
      LocationManagerTest  -[B4INativeObject RunMethod::] + 
    158
      LocationManagerTest  -[b4i_main _startbackground::] + 
    1114
      LocationManagerTest  -[b4i_main _application_start:] + 
    3128
      CoreFoundation       <redacted> + 
    68
      CoreFoundation       <redacted> + 
    292
      LocationManagerTest  +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 
    1784
     LocationManagerTest  -[B4IShell runMethod:] + 
    574
     LocationManagerTest  -[B4IShell raiseEventImpl:method:args::] + 
    2212
     LocationManagerTest  -[B4IShellBI raiseEvent:
    event:params:] + 1442
     LocationManagerTest  __
    33-[B4I raiseUIEvent:event:params:]_block_invoke + 74
     libdispatch.dylib    <redacted> + 
    10
     libdispatch.dylib    <redacted> + 
    22
     libdispatch.dylib    <redacted> + 
    1524
     CoreFoundation       <redacted> + 
    8
     CoreFoundation       <redacted> + 
    1574
     CoreFoundation       CFRunLoopRunSpecific + 
    520
     CoreFoundation       CFRunLoopRunInMode + 
    108
     GraphicsServices     GSEventRunModal + 
    160
     UIKit                UIApplicationMain + 
    144
     LocationManagerTest  main + 
    106
     libdyld.dylib        <redacted> + 
    2
    )
    Application_Active
    and this is my code:
    Code:
    #If OBJC

    - (BOOL)isSignificantLocationAvailable {
        if([CLLocationManager significantLocationChangeMonitoringAvailable]) {
          return YES;
        }
        return NO;
    }

    #end if

    Sub StartBackground(lm As LocationManager, MinimumDistance As Double)
        
    Dim no As NativeObject = lm
        
    Dim isSignLocAvailable As Boolean = no.RunMethod("isSignificantLocationAvailable"Null).AsBoolean
       
        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)

      
    If isSignLocAvailable = True Then
        no.RunMethod(
    "startMonitoringSignificantLocationChanges",Null)
      
    Else
            no.RunMethod(
    "startUpdatingLocation"Null)
      
    End If
    End Sub
     
  5. franchsesko

    franchsesko Member Licensed User

    Yeah, sorry, got it.
    I was not invoking the method on the correct object, here's my own fix.

    Code:
    Sub StartBackground(lm As LocationManager, MinimumDistance As Double)
        
    Dim no As NativeObject = lm
        
    Dim NativeMe As NativeObject = Me
        
    Dim isSignLocAvailable As Boolean = NativeMe.RunMethod("isSignificantLocationAvailable"Null).AsBoolean

        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)

      
    If isSignLocAvailable = True Then
        no.RunMethod(
    "startMonitoringSignificantLocationChanges",Null)
      
    Else
            no.RunMethod(
    "startUpdatingLocation"Null)
      
    End If
    End Sub
     
  6. b4xscripter

    b4xscripter Member Licensed User

    Hi!
    When I tried to publish my app in App Store, I got this issue:

    *******
    Your app declares support for location in the UIBackgroundModes key in your Info.plist file but does not have any features that require persistent location. Apps that declare support for location in the UIBackgroundModes key in your Info.plist file must have features that require persistent location.
    *******


    in my plist I have:


    Code:
    #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>
        
    #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>
    and my code is similar to the example in this thread:

    Code:
    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
    Code:
    Private Sub Application_Start (Nav As NavigationController)

        
        NavControl = Nav
        Page1.Initialize(
    "Page1")
        Page1.RootPanel.LoadLayout(
    "Page1")
        NavControl.ShowPage(Page1)
        
        App.RegisterUserNotifications(
    TrueTrueTrue'request permission for notifications
        App.ApplicationIconBadgeNumber = 0
        
        
    If App.LaunchOptions.IsInitialized Then
            
    Dim ln As Notification = App.LaunchOptions.Get("UIApplicationLaunchOptionsLocalNotificationKey")
            
    If ln.IsInitialized Then
                
    'hd.ToastMessageShow("Application was started from a notification: " & ln.AlertBody, True)
            End If
        
    End If
        
        fm.Initialize(
    "fm")
        WKWebView3.Loadurl(
    "http://85.88.153.23.nip.io:7095/promotions")
        
    Dim no As NativeObject = WKWebView3
        no.GetField(
    "scrollView").SetField("bounces"False)
        
        
        
        LocManager.Initialize(
    "LocManager")
        
        
    'in background....
        Dim no As NativeObject = LocManager
        no = no.GetField(
    "manager")
        no.SetField(
    "pausesLocationUpdatesAutomatically"False)
        StartBackground(LocManager, 
    0'replaces locManager.Start
        'in background....
        
        LocManager_AuthorizationStatusChanged(
    0)
        tn.Initialize(
    "tn")


    End Sub


    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




    Private Sub LocManager_LocationChanged (Location1 As Location)
        
    Dim job As HttpJob
        job.Initialize(
    "gps", Me)
        job.PostString(serverLink & 
    "/gps""lat=" & Location1.Latitude & "&lon=" & Location1.Longitude & "&speed=" & Location1.Speed & "&altitude=" & Location1.Altitude & "&os=" & "ios"  & "&userId=" & userId)
        
    Dim job As HttpJob
        job.Initialize(
    "getBeacons", Me)
        job.PostString(serverLink & 
    "/getBeacons""lat=" & Location1.Latitude & "&lon=" & Location1.Longitude & "&speed=" & Location1.Speed & "&altitude=" & Location1.Altitude )
    End Sub

    Why does Apple ask me that if I really use the app in background?

    I am a bit confused...

    Thank you for your help!
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread in the questions forum.
     
Loading...