anx-bridge

Overview

This is utility AIR Native Extension that allows to add asynchronous calls to other native extension.

Setup

Use com.github.airext.Bridge.swc library and corresponded headers to build ANE and com.github.airext.Bridge.ane for application that uses ANE based on anx-bridge.

Setup ActionScript project

Just link com.github.airext.Bridge.swc as external library.

Setup Objective-C project

  • link corresponded headers to your Xcode bin/headers/ios for iOS project and bin/headers/osx for OSX project;
  • in ContextInitializer function right after function initialization call [ANXBridge setup:functions:]; as showed next:
void TSTContextInitializer(void* extData, const uint8_t* ctxType, FREContext ctx, uint32_t* numFunctionsToSet, const FRENamedFunction** functionsToSet)
{
    *numFunctionsToSet = 1;
    
    FRENamedFunction* func = (FRENamedFunction*) malloc(sizeof(FRENamedFunction) * (*numFunctionsToSet));
    
    func[0].name = (const uint8_t*) "isSupported";
    func[0].functionData = NULL;
    func[0].function = &ANXTwitterIsSupported;
    
    [ANXBridge setup:numFunctionsToSet functions:&func];

    *functionsToSet = func;
}

this registers additional functions used by anx-bridge.

Setup Application project

<extensions> 
    <extensionID>com.github.airext.Bridge</extensionID>
</extensions>

Usage

ActionScript

Using anx-bridge from ActionScript in one line:

bridge(context).call("someMethod", arg1, arg2).callback(callback);

where:

  • context is ExtensionContext;
  • someMethod is registered function that takes two arguments arg1 and arg2;
  • callback is function with signature function(error:Error = null, value:Object = null):void.

Objective-C

On objective-c part you use [ANXBridge] class for create async calls and [ANXBridgeCall] class for notify failure or result:

// creates call
ANXBridgeCall* call = [ANXBridge call:context];

// notify success where value is FREObject received as second argument in callback on AIR side
[call result: value];

// notify failure where error is NSError that will be converted to ActionScript' Error object
[call reject:error];

// each async method should retrun ANXBridgeCall as FREObject
return [call toFREObject];

Example

Asume we implement Twitter login method (this is real example from fabric/twitter kit):

ActionScript part contains one method login:

public function login(callback:Function):void
{
  bridge(context).call("login").callback(callback);
}

Objective-C context initializer:

void ANXTwitterContextInitializer(void* extData, const uint8_t* ctxType, FREContext ctx, uint32_t* numFunctionsToTest, const FRENamedFunction** functionsToSet)
{
    *numFunctionsToTest = 1;
    
    FRENamedFunction* func = (FRENamedFunction*) malloc(sizeof(FRENamedFunction) * (*numFunctionsToTest));
    
    func[0].name = (const uint8_t*) "login";
    func[0].functionData = NULL;
    func[0].function = &ANXTwitterLogin;
    
    [ANXBridge setup:numFunctionsToTest functions:&func];

    *functionsToSet = func;
}

Objective-C context login method:

FREObject ANXTwitterLogin(FREContext context, void* functionData, uint32_t argc, FREObject argv[])
{
    ANXBridgeCall* call = [ANXBridge call:context];
    
    [[Twitter sharedInstance] logInWithCompletion:
        ^(TWTRSession *session, NSError *error)
        {
            if (session)
            {
              // assume there is method that converts TWTRSession to FREObject
              FREObject freSession = [self convertSessionToFREObject:session];
            
              [call result:freSession];
            }
            else
            {
                [call reject:error];
            }
        }];
    
    return [call toFREObject];
}

ActionScript usage in end application:

Twitter.sharedInstance().login(
  function(error:Error = null, session:Object = null):void
  {
    if (session != null)
    {
      trace("Logged in as:", session.userName);
    }
    else
    {
      trace("Login failed:", String(error));
    }
  }
);