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:
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:
Note that you can use Phone.SetFlashlight instead of this 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
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