Determine Exposed Methods Of An Externally Hosted SWF

Posted By : todd sharp Posted At : February 9, 2009 12:34 PM Posted In: Flex

1

Have you ever loaded up a SWF from an external source in Flex and wish that you knew what methods and variables it exposed? Most of the time you probably have a decent set of documents for the SWF that tell you this kind of information, but sometimes developers give you a means to embed their SWF but fail to provide any meaningful API with which to interact with the SWF. In the Java world we'd use the wonderful item known as 'reflection' to see the properties and methods of our class. With Flex it is a bit more complicated, but it can be done.

What follows is one way to figure out the exposed methods. I've only been working with Flex for a few months, so there may be an easier way.

The first step to figuring out the available methods is to find the available classes in the SWF. I used a sweet little utility called SWF Explorer to do that.

Once you have the class names it was a matter of the actual introspection to see the available methods. To do this you can use the getDefinition method of the ApplicationDomain class in Flex. Assuming that you loaded the SWF via a URLLoader named 'loader', your code might look something like this:

var c:Class = loader.contentLoaderInfo.applicationDomain.getDefinition(className) as Class;

However, that doesn't quite expose the methods. If you were to trace that you'd simply get back the name of the class. To figure out the methods you would then use the describeType method:

var c:Class = loader.contentLoaderInfo.applicationDomain.getDefinition(className) as Class;
//get the class definition

trace(describeType(c));

You should now have a super cool xml document giving you all the methods, variable names, etc. To call any of the methods you'd just cast the loader content as an object:

var remoteSWF:Object = loader.content as Object;
remoteSWF.remoteMethod();

Comments (1)

tit's Gravatar URLloader is not working, but SWFLoader with
var c:Class = swfLoader.loaderInfo.applicationDomain.getDefinition(className) as Class;