iOS Tutorial Inline Objective C code

moster67

Expert
Licensed User
I need to check an event using inline OBJC if it is possible.

Using the iLocation library and the Significant-Change Location Service (see my post here: https://www.b4x.com/android/forum/threads/background-location-tracking.50246/page-2#post-392042), it seems like I need to look out for an event which contains the UIApplicationLaunchOptionsLocationKey and then restart the Significant-Change Location Service (although from my test it seems to work nontheless). This is the information I found on Apple's web-site:

If you start this service and your app is subsequently terminated, the system automatically relaunches the app into the background if a new event arrives. In such a case, the options dictionary passed to theapplication:willFinishLaunchingWithOptions: and application:didFinishLaunchingWithOptions: methods of your app delegate contains the key UIApplicationLaunchOptionsLocationKey to indicate that your app was launched because of a location event. Upon relaunch, you must still configure a location manager object and call this method to continue receiving location events. When you restart location services, the current event is delivered to your delegate immediately. In addition, the location property of your location manager object is populated with the most recent location object even before you start location services.
I tried with the following code:

B4X:
Sub LocManager_didFinishLaunchingWithOptions (myvalue As Boolean)

    'test code
    Dim status As String
    If myvalue Then
        Log("value is true")
        status = "It worked - value is True"
    Else
        Log("value is false")
        status = "It worked - value is False"
    End If

    Dim ln As Notification
    ln.Initialize(DateTime.Now + 5 * DateTime.TicksPerSecond)
    ln.AlertBody = "Starting message: " & status
    ln.Register
    'end test code

    If myvalue = True Then
        StartBackground(locManager, 0)
    Else
        'do something
    End If


End Sub




#If OBJC

#import "iCore.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog(@"didFinishLaunchingWithOptions");

        // When there is a significant changes of the location,
        // The key UIApplicationLaunchOptionsLocationKey will be returned from didFinishLaunchingWithOptions
        // When the app is receiving the key, it must reinitiate the locationManager and get
        // the latest location updates

        // This UIApplicationLaunchOptionsLocationKey key enables the location update even when
        // the app has been killed/terminated (Not in th background) by iOS or the user. It works also when device
        // has been rebooted.

        NSLog(@"UIApplicationLaunchOptionsLocationKey : %@" , [launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]);

         [B4IObjectWrapper raiseEvent:self :@"_didFinishLaunchingWithOptions:" :@[]];
 
         if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) return YES;
            return NO;
      
}
    
#end if
The code compiles (both in debug and release mode) but the LocManager_didFinishLaunchingWithOptions will not trigger.

Obviously I am doing something wrong. Are you able to help?
 
Last edited:
Top