Access flex application through the SWFLoader component

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 <CDATAimport mx.core.IUIComponent; import mx.managers.SystemManager;</p>

		private var _systemManager:SystemManager;
		private var _innerApplication:IUIComponent;

		protected function loader_completeHandler(event:Event):void
		{
			_systemManager = SystemManager(loader.content);
		}
	]]>
</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.APPLICATIONCOMPLETE, sysManage_ApplicationComplete_Handler); systemManager.addEventListener(FlexEvent.UPDATECOMPLETE, 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.

Debug view of the application in Flash builder 4

Good luck!

Developer