iOS Tutorial Inline Objective C code

Discussion in 'iOS Tutorials' started by Erel, Jan 7, 2015.

Similar threads

B4i Tutorial Background playback
B4i Tutorial Notifications with actions
B4i Tutorial iBeacons
B4i Code Snippet Full screen apps
B4i Tutorial Share data from your app with ActivityViewController
  1. Erel

    Erel Administrator Staff Member Licensed User

    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:
    #If OBJC

    - (void)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:
    Sub SomeSub
    Dim NativeMe As NativeObject = Me
    "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.
    MarcoRome, JohnC, dexMilano and 7 others like this.
  2. LucaMs

    LucaMs Expert Licensed User


    (clean italian exclamation :), like "WOW")
    David Meier likes this.
  3. Ramezanpour

    Ramezanpour Active Member Licensed User

    Wow.... Its the best .... :X
  4. Erel

    Erel Administrator Staff Member Licensed User

    BTW, a similar feature will be added to B4A and B4J in their next versions.
    KZero, lemonisdead, nikolaus and 5 others like this.
  5. LucaMs

    LucaMs Expert Licensed User

    Inline Java code ;)
  6. susu

    susu Well-Known Member Licensed User

    Is there a way to use external library (.h file) on hosted builder?
  7. Erel

    Erel Administrator Staff Member Licensed User

    External libraries are made of two files (a and h). Currently it is not possible to add external libraries to the hosted builder.
  8. szy

    szy Member Licensed User

    webview component is to support the submission of post it?
  9. Erel

    Erel Administrator Staff Member Licensed User

    I'm not sure that I understand the question. Sorry.
  10. JakeBullet70

    JakeBullet70 Well-Known Member Licensed User

    DonManfred likes this.
  11. DonManfred

    DonManfred Expert Licensed User

    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
  12. Erel

    Erel Administrator Staff Member Licensed User

    It will take some time. Probably during February.
  13. DonManfred

    DonManfred Expert Licensed User

    Thank you. I need to be more patient :)
  14. hanyelmehy

    hanyelmehy Active Member Licensed User

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

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this question.
  16. moster67

    moster67 Expert Licensed 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".
    Hypnos and inakigarm like this.
  17. inakigarm

    inakigarm Well-Known Member Licensed 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 !!

    (and could invest my earned time to learn JavaScript maybe...)
    Hypnos likes this.
  18. Erel

    Erel Administrator Staff Member Licensed User

    It is better to post it in the wishlist forum.
  19. szy

    szy Member Licensed User

    Hoping to support SWIFT
  20. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    If I create a framework (mylibraries.framework for example), can I use "Inline way" to use my libraries without create a .a, .h and .xml?

    After framework is created, where I can put this to compile with B4i?


    Alberto Iglesias
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice