views:

238

answers:

1

I am working on an AIR application that needs to load, run, and access methods on a swf pulled in from the net. Using moduals has worked well in the past but due to design constraints is not possible for this application. Below you can see the code where I load the ImageTest.swf then call function Bleh() on it.

private var l:Loader = new Loader();
  private var ctx:LoaderContext;
  private function onInit():void
  {
   l.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete);    
   l.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onLoadError);    
   l.load(new URLRequest("ImageTest.swf"));
  }

  private function onLoadError(event:IOErrorEvent):void
  {

  }

  private function onLoadComplete(event:Event):void
  {
   ui.addChild(event.target.content);
   SystemManager(event.target.content).addEventListener(FlexEvent.APPLICATION_COMPLETE, swfAppComplete);
  }

  private function swfAppComplete(event:FlexEvent):void
  {
   var sys:SystemManager = SystemManager(event.currentTarget);
   var app:Object = sys.application;
   app.Bleh();
  }

This works fine when the swf is local to the AIR application, but when ImageTest.swf is off on a server, it loads fine but I get a coercion run time error(TypeError: Error #1034: Type Coercion failed: cannot convert _Engine_mx_managers_SystemManager@7c36281 to mx.managers.SystemManager) at the line:

SystemManager(event.target.content).addEventListener(FlexEvent.APPLICATION_COMPLETE, swfAppComplete);

I believe the error might be relating to a security sandbox issue, but I am not sure. Thanks in advance!

A: 

It is related to Security issue, your AIR app has APPLICATION Security sandbox, but loaded SWF doesn't. In order to get it working, you need to download SWF from the network into your AIR application, and then reload it with "allowLoadBytesCodeExecution" option.

The code will look like this:

    private function loadApp() : void {
        var urlLoader : URLLoader = new URLLoader();
        urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
        urlLoader.addEventListener( Event.COMPLETE, onAppLoaded );
        urlLoader.load( new URLRequest( url ) );
    }

    private function onAppLoaded( event : Event ) : void {
        var appLoader : Loader = new Loader();
        var context : LoaderContext = new LoaderContext();
        // don't assign currentDomain as applicationDomain here
        // it will UNABLE to unload swf later 
        context.applicationDomain = ApplicationDomain.currentDomain;
        context.allowLoadBytesCodeExecution = true;
        appLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, onAppExecuted );
        appLoader.loadBytes( ByteArray( ( event.currentTarget as URLLoader ).data ), context );
    }

    private function onAppExecuted( e : Event ) : void {
        content = SystemManager( LoaderInfo( e.target ).content );
        content.addEventListener( FlexEvent.APPLICATION_COMPLETE, onLoadedAppInitComplete );
    }
Sergey Z.