Adobe AIR读取本地外部SWF文件的功能概览

来源:互联网 发布:熊猫pdd淘宝小吃名字 编辑:程序博客网 时间:2024/05/21 04:16

来自印度同事发布的原文:
http://blogs.adobe.com/airodynamics/2012/11/09/packaging-and-loading-multiple-swfs-in-air-apps-on-ios/

下面是我的译文:
(译者,James Li,http://jamesli.cn)
———————————————-

In an upcoming release of AIR for iOS, a new feature is being planned that allows application developers to be able to package and load secondary SWFs that contain ActionScript byte code (or ABC) in their apps. Presently, due to certain restrictions on iOS, AIR on iOS requires that all the ActionScript code of an application be present in the main SWF or the root SWF. With this feature, an application developer can have ActionScript code in local secondary SWFs and package them along with the main SWF.

在接下来的AIR针对iOS平台的发布版本中,将有一个功能,允许应用开发者读取本地的(译者:指打包进IPA)含有ActionScript代码(或者ABC码)的SWF文件。在此之前,由于iOS平台的规则限制,AIR要求所有的ActionScript代码必须在主SWF或最外层的SWF里被审查到,(译者:也就是说无法将代码分配到第二个SWF中)。现在如果有了这个功能,开发者就可以分配一部分ActionScript代码到本地的SWF里,把它和主SWF一起打包进IPA中。

———————————————-

A bit of background

简单的背景介绍

AIR apps on iOS can be compiled either in AOT (ahead of time) mode or Interpreter mode. To know more about these modes, please refer to an earlier post on this blog: http://blogs.adobe.com/airodynamics/2012/07/04/aot-or-interpreter/

针对iOS平台,AIR应用程序既可以使用AOT模式(ahead of time)也可以使用解释器模式打包。想要了解详细细节,请参阅早先发布的这篇文章:
http://blogs.adobe.com/airodynamics/2012/07/04/aot-or-interpreter/

The AIR Developer Tool or ADT has targets that lets you package apps either for the AOT mode or Interpreter mode. The targets for packaging in AOT mode are ipa-app-store, ipa-ad-hoc, ipa-test and ipa-debug. The targets for packaging apps in interpreter mode are ipa-debug-interpreter, ipa-test-interpreter, ipa-debug-interpreter-simulator and ipa-test-interpreter-simulator. In the interpreter mode, as the SWF is interpreted and not translated to native machine code, hence it runs slower than when in AOT mode. All developers are advised that whenever they publish their apps on the AppStore, they should make sure that they are compiled in AOT mode. To read more about the ADT command and the iOS package targets please refer to: http://help.adobe.com/en_US/air/build/WS901d38e593cd1bac1e63e3d128cdca935b-8000.html

使用ADT(译者:ADT是使用命令行的AIR编译工具)可以选择以上两种打包模式。其中,支持AOT模式的target参数值为ipa-app-store,ipa-ad-hoc,ipa-test和ipa-debug;支持解释器模式的值为ipa-debug-interpreter,ipa-test-interpreter,ipa-debug-interpreter-simulator和ipa-test-interpreter-simulator。在解释器模式中,SWF文件被动态解释,并没有转换成本地的机器码,所以使用这种模式运行效率相对AOT模式要慢(译者:运算密集型应用比较明显)。我们建议所有的开发者在正式发布到应有商店时,都选择AOT模式。要了解ADT的命令和参数细节,请参阅这篇文章:
http://help.adobe.com/en_US/air/build/WS901d38e593cd1bac1e63e3d128cdca935b-8000.html

This feature affects only the AOT mode targets and the behavior of the interpreter mode targets is unchanged.

这个新的功能只对AOT模式有效。(译者:因为解释器模式原本就支持动态加载代码)

Until now, AIR required that when packaging in AOT mode, all the ActionScript code of an AIR iOS app should be present inside the main SWF. Any SWF other than the main SWF, that is provided at the time of packaging, is added to the final application package (IPA) as a resource. Any attempt to load a SWF (local or remote) dynamically that contains ActionScript byte code (or ABC) using either Loader.load() or Loader.loadBytes, resulted in an “Uncompiled ActionScript” error. A screenshot of the error dialog is below:

到现在位置,当使用AOT模式为iOS平台打包AIR应用,所有的ActionScript代码都必须要写在主SWF中。其他任何SWF文件,如果要打包入IPA,都被视为纯资源(译者:指不含代码的动画资源)。如果你使用了Loader.load()或者Loader.loadBytes强行从外部SWF文件(无论本地还是远程)中动态读取ActionScript代码(或者ABC代码),都会碰到这个窗口以示警告:



After the user dismisses the error dialog, the behavior of the application is “undefined”. Developers should never ship or publish an app in which the user may see such an error dialog.

如果用户关掉了这个窗口,应用的行为报一个“undefined”的错误。开发者永远不应该让用户看到这样的一个窗口。

Please note that if the SWF that is being loaded dynamically does not contain any ABC and contains only assets like bitmaps or audio, the SWF will be loaded successfully (even with AIR 3.4 and before). In fact, few developers have used this technique, to host asset-only SWFs on web servers and these are fetched only when needed. By downloading asset-SWFs on demand at run-time, developers have been able to reduce the size of their applications.

请注意如果读取的SWF中不含任何代码,只包含资源(位图或者音频),那么这个SWF是可以成功载入的(即使在3.4或者更早的版本)。实际上,一些开发者已经使用了这个技巧,从服务器上远程读取纯资源的SWF文件来替换本地的资源。这样可以减小安装包的体积。
———————————————-

What has changed ?

什么改变了?

In addition to the main SWF, the application developer can now provide a secondary SWF that contains ActionScript bytecode at the time of packaging and then load such a SWF dynamically using the functions provided by Loader. However, while dynamically loading a secondary SWF inside the main SWF, one needs to provide the application domain in which the secondary SWF should be loaded. The AOT mode in AIR on iOS does not support multiple application domains.

开发者现在可以在IPA内,在主SWF之外提供其他包含代码的SWF文件(译者:比如功能模块),并且使用Loader来动态读取。然而,当动态读取一个本地SWF文件时,需要指定加载的应用域(译者:平常情况下使用Loader读取并不一定需要这个参数)。目前AOT模式(针对iOS)还不支持多域加载。

Below is a code snippet for loading a secondary SWF that is packaged with the application.

下面是加载一个本地SWF的代码片段

123
var _urlRequest:URLRequest = new URLRequest(“mySecondarySwf.swf”);var _loader:Loader = new Loader();_loader.load(_urlRequest);


When the Loader.load() method does not specify a LoaderContext object, then by default, the SWF is loaded in an application domain that is a child of the loading SWF’s application domain. Multiple application domains are not supported in AIR on iOS, and hence the above code will not work on iOS and will result in the following error:

当使用Loader.load()方法时没有指定LoaderContext对象的值时,默认情况下SWF会被加载到加载者的子域中。因为刚才提到了AIR在iOS平台不支持多域加载,所以上面的代码运行会出错:

Error 3747: Multiple application domains are not supported on the operating system.

To overcome this error, the above code needs to provide ApplicationDomain.currentDomain as the application domain in the LoaderContext object. So the modified code is:

要解决这个错误,上面的代码需要在LoaderContext对象中指定ApplicationDomain.currentDomain的值。修改的代码如下:

1234
var _urlRequest:URLRequest = new URLRequest(“mySecondarySwf.swf”);var _loader:Loader = new Loader();var _lc:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, null);_loader.load(_urlRequest, _lc);


Loader.loadBytes

There is no change in the behavior of Loader.loadBytes() except for: In AOT mode, if the user attempts to load a SWF using Loader.loadBytes() and if the SWF version of the root SWF is greater than or equal to 18 (i.e. for the given build) then, the user needs to provide an application domain as part of the loader context which should be same as the root SWF’s application domain.

———————————————-
Loader.loadBytes

Loader.loadBytes()没有什么改变,只不过在AOT模式下,如果主SWF的版本大于等于18,当用户尝试使用Loader.loadBytes()读取一个本地SWF文件时,需要指定加载的应用域。

Loader.unload() & Loader.unloadAndStop()

The behavior of Loader.unload() and Loader.unloadAndStop() on iOS is below. When either of these functions is called the following will happen:

The contentLoaderInfo property of the Loader object will become null
Any assets that are loaded with the secondary SWF will be unloaded.
Any references to AS classes in the loaded SWF remain in memory, and new objects of those AS classes can be created. This is different from the behavior on other platforms.
A bit about what happens behind the scenes …
The ABC code from the main SWF and all the secondary SWFs is extracted and placed in the main executable. The non-ABC parts of a SWF or assets in a SWF are kept inside the IPA (as assets). They are drawn in memory when explicitly asked using the Loader.load() call.

———————————————-
Loader.unload() & Loader.unloadAndStop()

当在iOS平台调用Loader.unload()或者Loader.unloadAndStop()时,行为如下:
加载者对象的contentLoaderInfo属性为null
任何被加载SWF内的资源都被卸载
任何对被加载SWF的代码引用仍在内存中,并且这些类可以继续被创建。这一点和其他平台不同。
这是因为:主SWF和次SWF文件中的ABC代码都会被解压在主可执行文件中。SWF中的非ABC码或者资源部分都继续以资源的形式存在于IPA中。它们仅当调用Loader.load()时才被写入内存。
———————————————-

What will still not work?

什么还暂时不支持?

Dynamically loading a ‘remote‘ SWF, that contains ActionScript code will continue to NOT work.

远程动态加载一个含有代码的SWF还不能工作。
———————————————-

Where can i get the build?

到哪里获取这个版本?

The build with this feature enabled can be accessed here
This is the same build that was shared on the Labs (for AIR 3.5) a few weeks back. Please let us know if the feature is helpful to you and a bit about how you used it.

可以到这里获取含有这个功能的版本。这个版本和几周前在实验室里发布的版本相同(AIR 3.5测试版),如果你认为这个功能有帮助,请让我们知晓。
———————————————-
(译者:表格不翻译了,自己看原文)
———————————————-

Some FAQs

一些常用问题解答:

What is the loading order of ActionScript code among root SWF, secondary SWFs and ANEs?

在主SWF,次SWF和扩展中,ActionScript的初始化加载顺序是怎样的?

The loading order is ANE, followed by main SWF, followed by secondary SWFs. This implies that if there is a class with the same name that is defined in the main SWF as well as the secondary SWF, then the class definition of the main SWF will be used.

顺序依次是ANE,主SWF和次SWF。这意味着如果主SWF和次SWF使用了相同的类名,那么会使用主SWF中的类定义。

Will reloading of SWFs work?

SWF文件中支持重载么?

Due to certain restriction in the AOT toolchain workflow, once a secondary SWF with ABC code is loaded, it should not be reloaded. Unloading it will unload the assets in the secondary SWF. However, any attempt to reload the SWF may result in ‘random’ behavior.

由于AOT工具链流程的特定限制,当一个次SWF的ABC码被读取后,它就不能重载了。即使是调用了卸载程序,也只是卸载资源部分。任何针对重载的尝试都会带来不确定后果。

Will I be able to load remote SWFs that don’t contain any ActionScript code?

我可以远程加载不含代码的SWF文件么?

As evident from the above table, the answer to this is YES.

有证据证明,答案是肯定的。

Will I be able to use Workers API on iOS?

我可以在iOS中使用Workers多线程接口么?

ActionScript workers is not supported on iOS. Worker.isSupported on Mobile (both iOS and Android) would continue to return false.

iOS中还不支持ActionScript workers多线程接口。在移动设备中(iOS和Android),访问属性Worker.isSupported将继续返回false。

转自:http://jamesli.cn/blog/?p=1515

原创粉丝点击