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:

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

  • http://www.emttrainingcourse.com/ emt training

    nice post. thanks.

  • http://www.emttrainingcourse.com/ emt training

    nice post. thanks.

  • http://dobox.com/ Monica S.

    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.

  • http://dobox.com/ Monica S.

    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.

  • as3coder

    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!

    • http://e10.in e10 Infotech

      Did you happen to find any answer to this problem ?

      • http://www.kensodev.com Avi Tzurel

        Actually, I have no way in solving the problem or knowing even the source of it without testing or debugging the actual code.

        The best thing would be to post a stripped out version of the code to pastie.org or something with a few pointers on how I can test it and then maybe I can help.

        I never encountered this bug with the loader I posted, and this is a piece of code I am using in many apps, not just one.

  • as3coder

    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!

  • http://www.kensodev.com Avi Tzurel

    @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?

  • http://www.kensodev.com Avi Tzurel

    @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?

  • Adrian

    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:workflexdmv_automationprojectsdatavisualisationsrcmxchartsseriesAreaSeries.as:327]
    at mx.charts.chartClasses::ChartBase/get legendData()[C:workflexdmv_automationprojectsdatavisualisationsrcmxchartschartClassesChartBase.as:1161]
    at mx.charts::Legend/populateFromArray()[C:workflexdmv_automationprojectsdatavisualisationsrcmxchartsLegend.as:666]
    at mx.charts::Legend/commitProperties()[C:workflexdmv_automationprojectsdatavisualisationsrcmxchartsLegend.as:449]
    at mx.core::UIComponent/validateProperties()[C:autobuild3.2.0frameworksprojectsframeworksrcmxcoreUIComponent.as:5807]
    at mx.managers::LayoutManager/validateProperties()[C:autobuild3.2.0frameworksprojectsframeworksrcmxmanagersLayoutManager.as:539]
    at mx.managers::LayoutManager/doPhasedInstantiation()[C:autobuild3.2.0frameworksprojectsframeworksrcmxmanagersLayoutManager.as:659]
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at mx.core::UIComponent/callLaterDispatcher2()[C:autobuild3.2.0frameworksprojectsframeworksrcmxcoreUIComponent.as:8628]
    at mx.core::UIComponent/callLaterDispatcher()[C:autobuild3.2.0frameworksprojectsframeworksrcmxcoreUIComponent.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?

  • Adrian

    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?

  • Alexander

    There are 100000 forums with topics like this. But i couldnt find any explaining how you can access a function to a external loaded swf file and reverse.
    var loader:SWFLoader = new SWFLoader();
    With this type of loading swf file. Can you please explain how ?

    • http://www.kensodev.com Avi Tzurel

      The reverse is a problem, you can’t really access the SWF that is loading you, but for the SWF that is loading another one, you can access every function, UIComponent and class that is public with no problem at all.

      I have a solution like this that an application loading another application is showing the entire UI Tree of this loaded application.

  • http://www.facebook.com/profile.php?id=545933705 Thijs Triemstra

    Thanks for the information, was very useful while trying to load a Flex SWF into a Flash SWF.