How to run an external SWF inside a Flex Application?
Asked Answered
E

5

5

EDIT: Due to the answer I change the code posted. I've added the Security.allowDomain("*") line and that line throws me an error. So, how can that be made?

I want to run an Action Script 3.0 Application into a Flex Application. To do this I've done the following:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication windowComplete="loadSwfApplication()" xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:Script>
        <![CDATA[
            private function loadSwfApplication()
            {
                // The next line throws me an error.
                Security.allowDomain("*");

                var urlRequest:URLRequest = new URLRequest("path/to/the/application.swf");
                swfLoader.addEventListener(Event.COMPLETE, loadComplete);
                swfLoader.load(urlRequest);
            }

            private function loadComplete(completeEvent:Event)
            {
                var swfApplication:* = completeEvent.target.content;
                swfApplication.init();  // this is a Function that I made it in the Root class of swfApplication
            }
        ]]>
    </mx:Script>

    <mx:SWFLoader id="sfwLoader"/>

</mx:WindowedApplication>

The problem is that in the calling of swfApplication.init(); the AIR Player throws me an exception:

Security sandbox violation: caller file:///path/to/the/application.swf cannot access Stage owned by app:/SWFApplicationLoader.swf.

This is because somewhere in application.swf I use the stage like this:

if (root.stage != null)
    root.stage.addEventListener(Event.REMOVED, someFunction);
root.stage.stageFocusRect = false;

How can I load this swf application and USE the stage without any problems?

Eldredge answered 26/4, 2010 at 13:40 Comment(0)
J
18

You can try to load your SWF temporarily into a ByteArray and then load it with your SWFLoader.

Don't forget to set allowCodeImport to true since your SWF has as code inside.

Of course be sure that your loaded swf is secure enough for your application since it will have access at all your property.

private function loadSwfApplication():void {
  // load the file with URLLoader into a bytearray
  var loader:URLLoader=new URLLoader();

  // binary format since it a SWF
  loader.dataFormat=URLLoaderDataFormat.BINARY;
  loader.addEventListener(Event.COMPLETE, onSWFLoaded);

  //load the file
  loader.load(new URLRequest("path/to/the/application.swf"));
}
private function onSWFLoaded(e:Event):void {
 // remove the event
 var loader:URLLoader=URLLoader(e.target);
 loader.removeEventListener(Event.COMPLETE, onSWFLoaded);

 // add an Application context and allow bytecode execution 
 var context:LoaderContext=new LoaderContext();
 context.allowCodeImport=true;

 // set the new context on SWFLoader
 sfwLoader.loaderContext = context;

 sfwLoader.addEventListener(Event.COMPLETE, loadComplete);

 // load the data from the bytearray
 sfwLoader.load(loader.data);
}

// your load complete function
private function loadComplete(completeEvent:Event):void {
 var swfApplication:* = completeEvent.target.content;
 swfApplication.init();  // this is a Function that I made it in the Root 
                         // class of swfApplication
}
Jigaboo answered 4/5, 2010 at 12:41 Comment(2)
Does this work on mobile platforms too? If yes, you can write a Flash player for iOS.Mould
Any idea why this works with a byte array but not the swfloader component?Lysin
O
2

If they are being loaded from different domains you are going to have to add a security exception - http://livedocs.adobe.com/flex/3/html/help.html?content=05B_Security_08.html

if its being run locally youre probably going to have to add them to the list of trusted files or folders in the settings manager - http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html#117502

Offutt answered 26/4, 2010 at 15:31 Comment(2)
When I try to add the Security Exception it throws me an error: SecurityError: Error #3207: Application-sandbox content cannot access this feature. at flash.system::Security$/allowDomain()Gurgle
And It's already added to the trusted files in the settings manager.Gurgle
N
1

Assuming that the external SWF is also in the application directory, you could try loading it using the app:/ scheme:

var urlRequest:URLRequest = new URLRequest("app:/path/application.swf");

That may put it into the same security context as the main application.

Nga answered 2/5, 2010 at 9:50 Comment(0)
A
0

One thing you may want to consider is that if you are trying to run a SWF from inside your application directory in AIR, AIR restricts execution of files. If you copy the file to a temp file and the run it (along with allowLoadBytesCodeExecution set to true) then it works.

var file:File = File.applicationDirectory.resolvePath("myFile.swf");
this.tmpFile = File.createTempDirectory().resolvePath("myFile.swf");
file.copyTo(this.tmpFile);

imagePreview.loaderContext = lc;
imagePreview.source = tmpFile.url;
Aviatrix answered 13/11, 2012 at 20:21 Comment(0)
A
-1

it won't work for Flex Projectors.

Only we use SWFLoader and LocalConnection because they can communicate between external swf and main swf. Thanks for support!

Can you read my tutorial from Adobe's Forum

It is very better than MovieClip or Object-Callers

Thank for resolved solution :)

Best regards, Jens

Asperges answered 4/2, 2014 at 9:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.