Loading remote swf files in AIR
September 16th, 2008 Posted in air, coding, flexI am doing some research for a large AIR application that has to dynamically load swf files (these can be Flex modules, but also regular Flash movies). This is apparently not that easy to accomplish, because it doesn’t work the same as in a normal Flex web application. You quickly run in to security issues because your AIR application runs in a different security sandbox then the remote files you are trying to execute.
You might want to read the following posts before using the code:
- Remote Plugins and Modules in AIR: Excellent article by Ethan Malasky that explains the implications of loading external swf files into your AIR application.
- Loading Modules in AIR application: This solution worked for me and with some minor adjustments, I was also able to load a regular swf file (see code snippet).
Finding a proper solution is not easy. While the second post has a solution that is usable in some situations, it should be implemented with caution. A better solution that uses signing your Flex modules is described in the first post, but it is definitely harder to implement. For now I would go for the second approach.
The following code snippet loads a local swf file into your AIR application. The swf file has to be in the user’s local app-storage folder. The original code can be found in this post.
{
// File reference
var file:File;
file = File.applicationStorageDirectory.resolvePath(fileName);
// Open the SWF file
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
// Read SWF bytes into byte array and close file
var bytes: ByteArray = new ByteArray();
fileStream.readBytes(bytes);
fileStream.close();
// Prepare the loader context to avoid security error
var loaderContext:LoaderContext = new LoaderContext();
loaderContext.allowLoadBytesCodeExecution = true;
// Load the SWF file
var swfLoader: SWFLoader = new SWFLoader();
swfLoader.loaderContext = loaderContext;
swfLoader.source = bytes;
// Add to you stage
swfContainer.addChild(swfLoader);
}
Hopefully Adobe will add more support and techniques to achieve this in future versions of AIR. This would offcourse be very usefull when you want to create large enterprise applications.
4 Responses to “Loading remote swf files in AIR”
By jeremy mooer on Sep 30, 2008
I realize that because this is an old post you may never see this, but the feeling I got after speaking to someone from the AIR dev team was that they are intentionally trying to forbid developers from loading remote swfs just because there are too many potential security problems. I’m curious if you, or anybody out there, might know if this work-around (using your own SWFLoader w/ the correct LoaderContext + any random swf) could be closed off in future flash player versions.
By Alain on Sep 30, 2008
Hopefully Adobe will support a better way of dynamically loading swf files in the future.
While the above solution works, you should use it with care. I haven’t used it in any real world projects, but if you are in a controlled environment, then this could be solution.
By Terry Corbet on Nov 21, 2008
I would sure like to know who Jeremy spoke to. I cannot get a simple ’statement of direction’ from anyone on any forum at Adobe concerning the ‘best practice’ approach to modularization of an AIR application with specific regards to the use of the Flash Player’s ability to cache all the Flex framework infrastructure.
What has worked for the last four months breaks with SDK 3.2 and AIR 1.5. Now the attempt to locate the .swz files raises a 2032 error which shows a URL that has inserted in it a hokey [[DYNAMIC]] token which renders the actual URL invalid.
If anyone knows how to bridge the horrible gap between the folks on the AIR and the Flex sides of the SDK team, please, please get them to understand that ‘desktop applications’ — that are provisioned via the web, not by shipping CDs — are every bit as sensitive to the needless downloading of a quarter of a megabyte here and another quarter of a megabyte there, as any plain old webserver/Flex application.
By Alain on Nov 21, 2008
Hi Terry,
Thanx for sharing this. In the end the project was cancelled and I did not have to implement this. But it is good to know that this doesn’t work anymore with the new AIR 1.5.
I will investigate this further, and hopefully we will find a better solution in the future.