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.
 

susu

Well-Known Member
Licensed User
Longtime User
Is there a way to use external library (.h file) on hosted builder?
 

szy

Member
Licensed User
Longtime User
webview component is to support the submission of post it?
 

DonManfred

Expert
Licensed User
Longtime User
BTW, a similar feature will be added to B4A and B4J in their next versions.
When will the B4A version be released? I hardly cant wait and i really like to try this out as i´m in a java-coding-run actually :D
 

hanyelmehy

Active Member
Licensed User
Longtime User
can we use Inline Objective C code to show IAD
Programmatically creating a portrait banner view using xcode :
B4X:
ADBannerView *adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
[self.view addSubview:adView];
 

moster67

Expert
Licensed User
Longtime User
Would it be possible (sometime in the future) to have an "inline-Swift-code" feature implemented in B4i?

I know very well that our B4i-code is translated into Objective-C (and not Swift) but taking into account what outlined in the following article "Using Swift with Cocoa and Objective-C" (Mix and Match) perhaps it is possible unless there are technical reasons how B4i works which would not permit it.

Any thoughts about this?

I have been experimenting a bit with Swift and I find it much easier than Objective-C. This is probably because if you are familiar with Java and C# it has a similar "feeling".
 

inakigarm

Well-Known Member
Licensed User
Longtime User
Maybe not the place to comment (Wish forum?) but I strongly agree with monster67

I begun with B4A, remembering basic programming, next learned MYSQL, Next PHP, google Apis, next Java (strongly recommended to understand why certain things on B4A are made that way) and now, with inline objective-c, probably will need to understand objective-c ( very difficult to read for me )

If there's a chance to access native IOS objecst or making library's with swift (more easy understanding), it will be great !!

https://developer.apple.com/swift

(and could invest my earned time to learn JavaScript maybe...)
 
Top