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 an 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:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600">
	<mx:Script>
		<![CDATA[
			import mx.core.IUIComponent;
			import mx.managers.SystemManager;

			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>

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

_systemManager.addEventListener(FlexEvent.APPLICATION_COMPLETE, sysManage_ApplicationComplete_Handler);
_systemManager.addEventListener(FlexEvent.UPDATE_COMPLETE, sysManage_UpdateComplete_Handler);

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

			private function sysManage_UpdateComplete_Handler(event:FlexEvent):void
			{
				_innerApplication = _systemManager.application;
			}

			private function sysManage_ApplicationComplete_Handler(event:FlexEvent):void
			{
				_innerApplication = _systemManager.application;
			}

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 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!

Avi Tzurel

My name is Avi Tzurel. I'm a professional web developer from Israel. I spend most of my day developing both web products and RIA applications as well as imparting my experience onto others. I speak, teach and write about my passions, and develop applications according to what I preach. I specialize in Flex, Adobe Air, HTML, XHTML, Javascript, jQuery and other Javascript libraries, on the server side I do .net along side with Ruby on Rails. You can connect with me on Twitter or email me through the contact page on this blog.

  • Share/Bookmark
Posted Sunday, May 30th, 2010 under Flex, actionscript.

Tags: , ,

5 comments

  1. nice post. thanks.

  2. Hi, I’m very interested in Linux but Im a Super Newbie and I’m having trouble deciding on the right distribution for me (Havent you heard this a million times?) anyway here is my problem, I need a distribution that can switch between reading and writing in English and Japanese (Japanese Language Support) with out restarting the operating system.

  3. i have a real problem in this part of your code
    _systemManager = SystemManager(loader.content);

    the _systemManager object is always null! and the cast return an error TypeError: Error #1034 cannot covert _MySWFLOaded_mx_managers_SystemManager@2a791e9 into mx.managers.SystemManager , i’ve read a lot of posts.. and this problem appear in a lot of forums……..

    can you help me?
    the unique thing different in my code is thas the swfloader is creatad by actionscript!

  4. @as3coder
    Can you email me the code to avi AT kensodev DOT com or post the code to pastie.org so I can have a look?

  5. hi i have problem with swfLoader in flex, when i close following code in click method i can easly add many swf to main application

    public function doIT():void
    {
    var swfLoader1:SWFLoader = new SWFLoader;
    swfLoader1.source = “DayProfileElement.swf?wsdl=http://www.ecoscada.com/services/BuildingComponentService.asmx?wsdl&guid=fcf1dc38-2c74-46a7-8f4b-5bdda061088c”;
    swfLoader1.percentHeight = 100;
    swfLoader1.percentWidth = 100;
    swfLoader1.name = “gowno1″;
    swfLoader1.id = “gowno2″;

    var panel1:Panel = new Panel;
    panel1.percentHeight = 100;
    panel1.percentWidth = 100 / 4;
    panel1.name =”gowno3″;
    panel1.id = “gowno4″;
    panel1.addChild(swfLoader1);

    content.addChild(panel1);
    }

    it work when i push the button like once per second, but when i do it very fast i have following error:
    TypeError: Error #1034: Type Coercion failed: cannot convert mx.core::ClassFactory@a93a671 to mx.core.IFactory.
    at mx.charts.series::AreaSeries/get legendData()[C:\work\flex\dmv_automation\projects\datavisualisation\src\mx\charts\series\AreaSeries.as:327]
    at mx.charts.chartClasses::ChartBase/get legendData()[C:\work\flex\dmv_automation\projects\datavisualisation\src\mx\charts\chartClasses\ChartBase.as:1161]
    at mx.charts::Legend/populateFromArray()[C:\work\flex\dmv_automation\projects\datavisualisation\src\mx\charts\Legend.as:666]
    at mx.charts::Legend/commitProperties()[C:\work\flex\dmv_automation\projects\datavisualisation\src\mx\charts\Legend.as:449]
    at mx.core::UIComponent/validateProperties()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:5807]
    at mx.managers::LayoutManager/validateProperties()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\LayoutManager.as:539]
    at mx.managers::LayoutManager/doPhasedInstantiation()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\LayoutManager.as:659]
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at mx.core::UIComponent/callLaterDispatcher2()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8628]
    at mx.core::UIComponent/callLaterDispatcher()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8568]

    when i close adding those swfs in loop i have the same error, it look like hild apps couldn’t access
    certain objects that were instantiated by another child app, so what can i do?

Leave a Reply