Access flex application through the SWFLoader component

Posted on May 30, 2010

As you know, I’m a web developer and I love learning new stuff and blogging about it here. My blog post for today is about access flex application through the SWFLoader component.

Often you load flex applications through another flex application using the SWFLoader component.

It’s not that common knowledge that you can actually access all of that application exactly the same way you are accessing your own application.

What does that mean?

It means you can access that application components, get the children and get a good knowledge on how this application is build. You can also access it’s component and dispatch events.

OK, how?

Let’s start doing it. We will create a simple application, put a SWFLoader on stage and create a listener for the complete event.

Lets’s also add 2 global variables – one for SystemManager, another for IUIComponent (I will explain later).

This is what the application code should look like:

[actionscript3]

<mx:Application xmlns:mx=“http://www.adobe.com/2006/mxml" layout=“absolute” minWidth=“955” minHeight=“600”>

mx:Script

</mx:Script>

<mx:SWFLoader id=“loader” source=“VistaRemix.swf” width=“800” height=“600” autoLoad=“true” complete=“loader_completeHandler(event)"/>

</mx:Application>

[/actionscript3]

As you can probably see, we are assigning a value to _systemManager when the loader (SWFLoader) finished loading. This enables us to work with that application system Manager and access properties.

Continuing…

We will add 2 Event listeners inside that function:

[actionscript3]

_systemManager.addEventListener(FlexEvent.APPLICATION_COMPLETE, sysManage_ApplicationComplete_Handler);

_systemManager.addEventListener(FlexEvent.UPDATE_COMPLETE, sysManage_UpdateComplete_Handler);

[/actionscript3]

Now that we have those in place, let’s add the closure functions:

[actionscript3]

private function sysManage_UpdateComplete_Handler(event:FlexEvent):void

{

_innerApplication = _systemManager.application;

}

private function sysManage_ApplicationComplete_Handler(event:FlexEvent):void

{

_innerApplication = _systemManager.application;

}

[/actionscript3]

Great! Now _innerApplication is actually an application. You can get it’s children, get properties, get components, dispatch events and more.

This comes in very handy when you want control over an application and you don’t have the source. You can create a clone of that application and create your own GUI for it. It’s an amazing feature!

This here is a screenshot of debugging mode. Check out the debugger watch section, these are all methods you can use on that variable, you can of course use much more.

Good luck!