here is basic sample how you could achieve that. Note: I'm using mxml syntax and components - but the *KEY* is api use of LoaderContext - not what real components are used to model UI.
Say I have host:
<?xml version="1.0" encoding="utf-8"?>
protected function creationCompleteHandler(event:FlexEvent):void
var loaderContext:LoaderContext = new LoaderContext();
loaderContext.allowCodeImport = true;
swfLoader.loaderContext = loaderContext;
<s:SWFLoader id="swfLoader" width="100%" height="100%"/>
MyLoadedApp is not created with air SDK at all - it is just flash.display.Sprite movie clip created with any compatible Flash compiler:
public class MyLoadedApp extends Sprite
public function MyLoadedApp()
protected function initApp(addedToStageEvent:Event):void
var clazz:Class = flash.utils.getDefinitionByName("flash.filesystem.File") as Class;
// we could read documents directory (File.documentsDirectory) using class access
var directory:Object = clazz.documentsDirectory;
// and we could show browse dialog (now we are using instance - not class)
directory.browseForDirectory("Please select directory");
} catch (error:Error)
// handle error
var errorText:TextField = new TextField();
errorText.autoSize = "left";
errorText.text = error.message;
protected function directorySelectedHandler(selectEvent:Event):void
var directory:Object = selectEvent.target;
var files:Array = directory.getDirectoryListing();
var resultsTextField:TextField = new TextField();
resultsTextField.autoSize = "left";
resultsTextField.multiline = true;
for(var i:uint = 0; i < files.length; i++)
This is not safe at all and loaded module can do everything I think (we talk about local modules from app storage, like in Zinc, right?)
Thanks Peter, yes using getDefinitionByName would be a way to do it but it is a bit heavy as I am coding quite a large app.
Am I correct to suspect that only the SWF that is published as an AIR file is allowed to compile AIR classes ? I guess that it would make sense in term of security to prevent externally loaded SWF to trigger malicious code using the AIR API but it is not very flexible.
I find it very frustrating as the only easy way I have found for the loaded SWFs to interact with the AIR API is to bubble events up to the AIR container and to call methods directly on that container, not very flexible.
What I cannot get my head around is how to develop a modular application using AIR if the loaded SWFs cannot access the AIR API directly but only through the AIR container.
All sorted it was a false problem. I just added airglobal.swc to the library path in Flash CS5 and made the library as EXTERNAL it compile fine now, phew!