Skip to main content
Known Participant
May 13, 2014
Answered

Loading external swf into Air App

  • May 13, 2014
  • 1 reply
  • 4695 views

Hi All,

I'm building an Adobe Air App for desktop and am having problems loading an external swf. Every time i try to load i get:

SecurityError: Error #2070: Security sandbox violation........ cannot access Stage owned by app....blah blah

The file that is being loaded is in a local directory, but as i understand its in a different 'sandbox' which is a security risk.


Is there any way around this?!


Any help would be much appreciated


Many Thanks


Matt

This topic has been closed for replies.
Correct answer kglad

Hi Kglad, Thanks for the Example.

if i run my class with securityDomain = SecurityDomain.currentDomain; then it throws this error: SecurityError: Error #2142: Security sandbox violation: local SWF files cannot use the LoaderContext.securityDomain property.

The swf file im trying to load is stored locally so i guess this error makes sense. However if i comment out that line i get the same "cannot access Stage owned by app" error?

See below for class:

package  {

  import flash.display.MovieClip;

  import flash.filesystem.File;

  import flash.events.Event;

  import flash.net.FileReference;

  import flash.events.MouseEvent;

  import flash.display.Loader;

  import flash.net.URLRequest;

  import flash.system.LoaderContext;

  import flash.system.ApplicationDomain;

  import flash.system.SecurityDomain;

  public class assetPreview extends MovieClip {

  private var loader:Loader;

  private var mainSWF:MovieClip = new MovieClip();

  public function assetPreview() {

  addEventListener(Event.ADDED_TO_STAGE, initialise);

  }

  public function initialise(e:Event):void

  {

  removeEventListener(Event.ADDED_TO_STAGE, initialise);

  var allowSWF:LoaderContext = new LoaderContext(true,ApplicationDomain.currentDomain);

  allowSWF.securityDomain = SecurityDomain.currentDomain;

  loader = new Loader();

  loader.load( new URLRequest(settingsXML.pathToSWF),allowSWF);

  loader.contentLoaderInfo.addEventListener(Event.COMPLETE, viewPreview);

  }

  public function viewPreview(e:Event):void

  {

  addChild(mainSWF);

  mainSWF.addChild(loader);

  }

  }

}


that's why i added those comments about the swf's domain.  for locally loaded swfs, use:

SFMltd wrote:

Hi Kglad, Thanks for the Example.

if i run my class with securityDomain = SecurityDomain.currentDomain; then it throws this error: SecurityError: Error #2142: Security sandbox violation: local SWF files cannot use the LoaderContext.securityDomain property.

The swf file im trying to load is stored locally so i guess this error makes sense. However if i comment out that line i get the same "cannot access Stage owned by app" error?

See below for class:

package  {

  import flash.display.MovieClip;

  import flash.filesystem.File;

  import flash.events.Event;

  import flash.net.FileReference;

  import flash.events.MouseEvent;

  import flash.display.Loader;

  import flash.net.URLRequest;

  import flash.system.LoaderContext;

  import flash.system.ApplicationDomain;

  import flash.system.SecurityDomain;

  public class assetPreview extends MovieClip {

  private var loader:Loader;

  private var mainSWF:MovieClip = new MovieClip();

  public function assetPreview() {

  addEventListener(Event.ADDED_TO_STAGE, initialise);

  }

  public function initialise(e:Event):void

  {

  removeEventListener(Event.ADDED_TO_STAGE, initialise);

  var allowSWF:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);

// allowSWF.securityDomain = SecurityDomain.currentDomain;

  loader = new Loader();

  loader.load( new URLRequest(settingsXML.pathToSWF),allowSWF);

  loader.contentLoaderInfo.addEventListener(Event.COMPLETE, viewPreview);

  }

  public function viewPreview(e:Event):void

  {

  addChild(mainSWF);

  mainSWF.addChild(loader);

  }

  }

}

1 reply

kglad
Community Expert
Community Expert
May 13, 2014

you're using a loadercontext, correct?  is it using currentDomain?

SFMltdAuthor
Known Participant
May 13, 2014

Hi Kglad,

I have tried using a loaderContext with currentDomain but I couldn't get it to work. Could you give me an example of the correct approach? Perhaps I was using it wrongly with the loader instance.

Many Thanks

Matt

kglad
Community Expert
Community Expert
May 14, 2014

var loaderContext=new LoaderContext(true,ApplicationDomain.currentDomain);  //<-if you're loading a swf from a different domain, load policy file.  ie, you need a policy file on the loaded swf's server

loaderContext.securityDomain=securityDomain.currentDomain;  // "  ".

var loader:Loader=new Loader();

loader.load(new URLRequest('yourswf.swf',loaderContext);