By Abhinav



8 March 2013

Starting with AIR 3.7, application developers are able to host their secondary SWFs on an external server and load them on demand as per their application logic.
Till AIR 3.5, loading of only assets such as images, videos and SWFs without actionscript code, commonly referred as Actionscript Byte Code(ABC Code), from external server was supported. The workflow for the application developer for loading such assets from server resembles the diagram below: 
Workflow for uploading assets from server
With AIR 3.6, the feature for loading of locally packaged secondary SWFs containing ABC code was introduced. The detailed description about this feature and its usage can be found in the article – “Packaging and loading of multiple SWFs for AIR apps on iOS“. The workflow for the application developer using this feature is described in the diagram below:
Workflow for the app developer
With AIR 3.7, the feature for external hosting of secondary SWFs has been introduced. The workflow for the application developers who want to use this feature would change slightly from AIR 3.6 and is shown below :
Workflow for app developer using AIR 3.7
The developer needs to provide the url of the secondary stripped SWF in the URLRequest. The stripped SWF is obtained after the ADT packaging as described later in the article. A sample URL request for using this feature is :

private var externalSwfUrl:String= "";

private var urlRequest:URLRequest = new URLRequest(externalSwfUrl);

To use this feature, developer needs to specify a text file containing details of the SWF files to be stripped & externally hosted. This text file contains line separated paths of SWFs, which are to be externally hosted. A sample text file describing three SWFs to be externally hosted  would look like :
The name of the text file needs to be mentioned in the tag within the tag in the application descriptor as shown below:


During command line packaging using AIR Developer Tool(ADT), the developer needs to specify the text file just like an asset along with the set of SWF’s mentioned in the text file. A sample ADT command for using this feature is:

~/bin/adt -package -target ipa-app-store -provisioning-profile -storetype pkcs12 -keystore -storepass ResultantIPA.ipa SampleMainSwf-app.xml SampleMainSwf.swf assets/SampleSWFInfoFile.txt assets/Level1.swf assets/Level2.swf assets/Level3/asset/Level3.swf

During IPA packaging,  ADT extracts the ABC code from all child SWFs, adds it to the final executable and generates stripped SWFs in the “externalStrippedSwfs” folder created in the current working directory. The directory structure within the “externalStrippedSwfs” folder remains the same as specified within the text file. The generated stripped SWF’s should then be externally hosted on a web server of developer’s choice.
NOTE: The “externalStrippedSwfs” folder is not generated in the currently available release of Flash Builder 4.7/ Flash CS6. To enable generation of Stripped SWFs for external hosting, command line ADT packaging should be used.
A sample actionscript code which loads a secondary SWF  from an external server is described below:

package { import flash.display.Loader; import flash.display.Sprite; import; import; import; import flash.system.ApplicationDomain; import flash.system.LoaderContext; public class SampleMainSwf extends Sprite { private var externalLoader:Loader; private var url:String= ""; private var urlRequest:URLRequest = new URLRequest(url); private var ldrContext:LoaderContext; public function SampleMainSwf() { externalLoader = new Loader(); externalLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler); externalLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,errorHandler); ldrContext=new LoaderContext(false,ApplicationDomain.currentDomain,null); try { externalLoader.load(urlRequest, ldrContext); } catch(e:Error) { trace("Error ID : "+e.errorID+"\nError Message : "+e.message); } } private function completeHandler(e:Event):void { addChild(externalLoader); } private function errorHandler(e:IOErrorEvent):void { trace("In IO ErrorEvent Handler"); } } }

Points to note

  1. Conflicting symbols (classes and global functions) in all the packaged SWFs must be avoided as this might result into undesired behaviour.
  2. The stripped SWFs generated using this feature are different from pure asset SWFs. It must be noted that while pure asset SWFs are valid SWFs, the stripped SWFs are invalid SWFs.
  3. The SWF version of the ROOT.SWF must be greater than or equal to twenty (>= 20), to use this feature. If the SWF version is set to less than twenty (<20), the stripped SWFs won’t be generated in the “externalStrippedSwfs” folder even if the SWF files have been specified in the text file.
  4. It is recommended that this feature should not be used as a means for updating and displaying content in a dynamic manner. The externally hosted SWFs should not be replaced by any other stripped SWFs except the ones generated during IPA packaging. Any change in the secondary SWF requires a corresponding updation of stripped SWFs on external server.
  5. All the SWFs specified in the text file should be passed during the ADT packaging as well.

Known issues

External hosting of Flex SWF does not work and will cause the application to crash.