iOS Tutorial Inline Objective C code

B4i v1.80 adds support for inline Objective C code. This means that you can add Objective C code to your modules and they will be compiled together with all other code.
You can then use NativeObject to call the methods that you added.

The native code cannot be debugged.

The Objective C code should be added inside a condition block:
B4X:
#If OBJC

- (void)test {
   NSLog(@"test");
}

#End If
OBJC is a special symbol. You do not need to add it to the build configuration symbols.
The compiler will take the code from all these blocks and will add it at the end of the module (right before @end).

You can also add #imports. #imports will be added to the beginning of the generated module automatically.

For example, you can turn on the flash with this code:
B4X:
Sub SomeSub
 Dim NativeMe As NativeObject = Me
 NativeMe.RunMethod("turnTorchOn:", Array (true))
End Sub


#If ObjC
#import <AVFoundation/AVFoundation.h>
- (void) turnTorchOn: (bool) on {

  // check if flashlight available
  Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
  if (captureDeviceClass != nil) {
  AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  if ([device hasTorch] && [device hasFlash]){

  [device lockForConfiguration:nil];
  if (on) {
  [device setTorchMode:AVCaptureTorchModeOn];
  [device setFlashMode:AVCaptureFlashModeOn];
  //torchIsOn = YES; //define as a variable/property if you need to know status
  } else {
  [device setTorchMode:AVCaptureTorchModeOff];
  [device setFlashMode:AVCaptureFlashModeOff];
  //torchIsOn = NO;   
  }
  [device unlockForConfiguration];
  }
  } }
#end if
Note that you can use Phone.SetFlashlight instead of this code.
 

moster67

Expert
Licensed User
Longtime 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:

Ju Yang

Active Member
Licensed User
Longtime User
hello, can I use a third Party framework by inline object-c? I really need It
 
Top