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
     
Loading...