我嘗試下載外部SWF並在AIR安全沙箱中運行它。在AIR Security Sandbox中執行遠程SWF
這是AIR應用程序的代碼:
public class Downloader extends Sprite
{
private static const REMOTE_FILE:URLRequest = new URLRequest("http://myserver.com/downloadable.swf");
private var _main:NativeWindow;
public function Downloader()
{
var loader:URLLoader = new URLLoader(REMOTE_FILE);
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, downloadComplete);
}
private function downloadComplete(e:Event):void{
var ba:ByteArray = e.target.data;
var stream:FileStream = new FileStream();
var file:File = File.applicationStorageDirectory.resolvePath("downloadable.swf");
stream.open(file, FileMode.WRITE);
stream.writeBytes(ba);
stream.close();
loadAndRunSwf();
}
private function loadAndRunSwf(){
this._main = new NativeWindow();
this._main.width = 1024;
this._main.height = 768;
////obsolete?
//var context:LoaderContext = new LoaderContext();
//context.allowLoadBytesCodeExecution = true;
//context.applicationDomain = ApplicationDomain.currentDomain;
var file:File = File.applicationStorageDirectory.resolvePath("downloadable.swf");
var loader:Loader = new Loader();
loader.load(new URLRequest(file.url)/*,context*/);
this._main.stage.addChild(loader);
this._main.activate();
}
}
的downloadable.swf的代碼:
public class Downloadable extends Sprite
{
private var _btn:Button = new Button();
private var _baseFolder:File = new File("app-storage:/");
public function downloadable_test()
{
this.addChild(_btn);
_btn.label = "access Harddisk";
...
}
}
所以現在,如果我運行下載器,它會下載SWF文件,並嘗試運行它,但我會在下載得到一個異常就行
private var _baseFolder:File = new File("app-storage:/");
錯誤:
SecurityError: file
at runtime::SecurityManager$/checkPrivilegeForCaller()
那麼 - 我需要做什麼來防止這種安全錯誤?我希望將我的遠程SWF視爲在與AIR代碼相同的安全沙箱中運行的本機代碼。
我有一個類似的問題作爲墊。使用SecurityDomain不起作用=我遇到以下錯誤:SecurityError:錯誤#2142:安全沙箱衝突:本地SWF文件無法使用LoaderContext.securityDomain屬性。 app:/game.swf正在嘗試加載應用程序存儲:/levels/00_intro.swf。 – Oldes 2012-01-15 20:55:33
你是對的......在這裏和那裏弄亂了代碼後,Loader.loadBytes()似乎工作到目前爲止。謝謝。 – Oldes 2012-01-16 17:22:13
雖然它看起來這個技巧只適用於PC ..不適用於Android:/ – Oldes 2012-01-17 16:28:32