如何处理 Loader.load() 中的安全错误 #2070 和 #2000
How to handle the SecurityError #2070 and #2000 in Loader.load()
我有一个加载任何 swf 的 AIR 项目。但是当加载的 swf 发出 SecurityError 时,我无法处理这个错误!我的代码如下:
var loader:Loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfComplete);
_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, swfErrors);
_loader.contentLoaderInfo.addEventListener(Event.INIT, swfInit);
_loader.contentLoaderInfo.addEventListener(Event.OPEN, swfOpen);
_loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatus);
_loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, swfSecurityError);
_loader.contentLoaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrors);
try {
_loader.load(new URLRequest('path.swf'));
}
catch(e:SecurityError) {
trace("trying handle error!");
}
function swfComplete(e:Event):void {
trace("complete");
}
function swfErrors(e:IOErrorEvent):void {
trace("swf error: " + e.toString() );
}
function swfInit(e:Event):void {
trace("swf init");
}
function swfOpen(e:Event):void {
trace("swf open. this listener dispatch!");
}
function httpStatus(e:HTTPStatusEvent):void {
trace("http status: " + e.toString() );
}
function swfSecurityError(e:SecurityErrorEvent):void {
trace("trying handle security error. :(");
}
function uncaughtErrorEvent(e:UncaughtErrorEvent):void {
trace("uncaught error: " + e.toString() );
}
FlashDevelop 异常:
[Fault] exception, information=SecurityError: Error #2070: Security sandbox violation: caller file.swf cannot access Stage owned by app:/myApp.swf.
[Fault] exception, information=SecurityError: Error #2000: No active security context.
问题是我的程序退出了,我无法捕获这些安全错误。如何防止我的程序关闭?
您的代码无法处理此异常,因为它不是加载例程冲突。当加载的内容试图访问舞台(并且由于沙盒政策而无权访问它)时,就会发生这种情况。
您可以(可能)通过几种方式解决它:
- 通过 URLLoader 将您的文件作为二进制数据加载,然后 Loader.loadBytes 它。因此,您的内容将被视为同一个沙箱中的内部内容,并且它可以完全访问您的整个应用程序。
- 处理异常:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/UncaughtErrorEvent.html
- 将该 SWF 作为附加文件添加到您的应用中,使其与主 SWF 位于同一文件夹中,并且也将加载到同一沙箱中。不能肯定,因为我从未发布过 FD 的 AIR 应用程序。
我有一个加载任何 swf 的 AIR 项目。但是当加载的 swf 发出 SecurityError 时,我无法处理这个错误!我的代码如下:
var loader:Loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfComplete);
_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, swfErrors);
_loader.contentLoaderInfo.addEventListener(Event.INIT, swfInit);
_loader.contentLoaderInfo.addEventListener(Event.OPEN, swfOpen);
_loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatus);
_loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, swfSecurityError);
_loader.contentLoaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrors);
try {
_loader.load(new URLRequest('path.swf'));
}
catch(e:SecurityError) {
trace("trying handle error!");
}
function swfComplete(e:Event):void {
trace("complete");
}
function swfErrors(e:IOErrorEvent):void {
trace("swf error: " + e.toString() );
}
function swfInit(e:Event):void {
trace("swf init");
}
function swfOpen(e:Event):void {
trace("swf open. this listener dispatch!");
}
function httpStatus(e:HTTPStatusEvent):void {
trace("http status: " + e.toString() );
}
function swfSecurityError(e:SecurityErrorEvent):void {
trace("trying handle security error. :(");
}
function uncaughtErrorEvent(e:UncaughtErrorEvent):void {
trace("uncaught error: " + e.toString() );
}
FlashDevelop 异常:
[Fault] exception, information=SecurityError: Error #2070: Security sandbox violation: caller file.swf cannot access Stage owned by app:/myApp.swf.
[Fault] exception, information=SecurityError: Error #2000: No active security context.
问题是我的程序退出了,我无法捕获这些安全错误。如何防止我的程序关闭?
您的代码无法处理此异常,因为它不是加载例程冲突。当加载的内容试图访问舞台(并且由于沙盒政策而无权访问它)时,就会发生这种情况。
您可以(可能)通过几种方式解决它:
- 通过 URLLoader 将您的文件作为二进制数据加载,然后 Loader.loadBytes 它。因此,您的内容将被视为同一个沙箱中的内部内容,并且它可以完全访问您的整个应用程序。
- 处理异常:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/UncaughtErrorEvent.html
- 将该 SWF 作为附加文件添加到您的应用中,使其与主 SWF 位于同一文件夹中,并且也将加载到同一沙箱中。不能肯定,因为我从未发布过 FD 的 AIR 应用程序。