Specifying loading context

When you load an external file into OpenFL through the load() or loadBytes() method of the Loader class, you can optionally specify a context parameter. This parameter is a LoaderContext object.

The LoaderContext class includes three properties that let you define the context of how the loaded content can be used:

  • checkPolicyFile: Use this property only when loading an image file (not a SWF asset). If you set this property to true, the Loader checks the origin server for a policy file. This is necessary only for content originating from domains other than that of the project containing the Loader object. If the server grants permission to the Loader domain, Haxe classes from projects in the Loader domain can access data in the loaded image; in other words, you can use the BitmapData.draw() command to access data in the loaded image.

Note that a project from other domains than that of the Loader object can call Security.allowDomain() to permit a specific domain.

  • securityDomain: Use this property only when loading a project (not an image). Specify this for a project from a domain other than that of the file containing the Loader object. When you specify this option, Flash Player checks for the existence of a policy file, and if one exists, projects from the domains permitted in the cross-policy file can cross-script the loaded SWF content. You can specify openfl.system.SecurityDomain.currentDomain as this parameter.
  • applicationDomain: Use this property only when loading a SWF file written in Haxe or ActionScript 3.0 (not an image or a SWF file written in ActionScript 1.0 or 2.0). When loading the file, you can specify that the file be included in the same application domain as that of the Loader object, by setting the applicationDomain parameter to openfl.system.ApplicationDomain.currentDomain. By putting the loaded project in the same application domain, you can access its classes directly. This can be useful if you are loading a project that contains embedded media, which you can access via their associated class names.

Here’s an example of checking for a policy file when loading a bitmap from another domain:

var context = new LoaderContext ();
context.checkPolicyFile = true;
var urlReq = new URLRequest ("http://www.[your_domain_here].com/photo11.jpg");
var ldr = new Loader ();
ldr.load (urlReq, context);

Here’s an example of checking for a policy file when loading a SWF from another domain, in order to place the file in the same security sandbox as the Loader object. Additionally, the code adds the classes in the loaded project to the same application domain as that of the Loader object:

var context = new LoaderContext ();
context.securityDomain = SecurityDomain.currentDomain;
context.applicationDomain = ApplicationDomain.currentDomain;
var urlReq = new URLRequest ("http://www.[your_domain_here].com/library.swf");
var ldr = new Loader ();
ldr.load (urlReq, context);

For more information, see the LoaderContext class in the API Reference.

results matching ""

    No results matching ""