3 Replies Latest reply on Jun 9, 2011 10:59 AM by Calwen

    Accessing AIR API from loaded SWFs

    Calwen Level 1

      I am relatively new to AIR development and I can't get my head a round a little issue. I have an AIR application created in Flash CS5 that is just a shell that loads modules on demand, those modules are just SWF.

       

      I would like to access the AIR API directly from the classes of those SWF but of course they don't compile because in the publish settings they are just exported for Flash player 10 and don't see the AIR API. I tried adding airglobal.swc to the library hoping it would sort out the compiling problem but it doesn't and it generates many errors.

       

      I use to develop with MDM ZInc and I would just import the MDM SWC in any classes that would want access the MDM API, I am unsure how to proceed with AIR.

       

      Any help would be appreciated.

       

      Calwen

        • 1. Re: Accessing AIR API from loaded SWFs
          Peter Blazejewicz Level 4

          Hi Calwen,

           

          here is basic sample how you could achieve that. Note: I'm using mxml syntax and components - but the *KEY* is api use of LoaderContext - not what real components are used to model UI.

          Say I have host:

           

           

          <?xml version="1.0" encoding="utf-8"?>

          <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"

                                      xmlns:s="library://ns.adobe.com/flex/spark"

                                      xmlns:mx="library://ns.adobe.com/flex/mx"

                                      creationComplete="creationCompleteHandler(event)">

               <fx:Script>

                    <![CDATA[

                         import mx.events.FlexEvent;

                         //

                         protected function creationCompleteHandler(event:FlexEvent):void

                         {

                              var loaderContext:LoaderContext = new LoaderContext();

                              loaderContext.allowCodeImport = true;

                              swfLoader.loaderContext = loaderContext;

                              swfLoader.load("app:/MyLoadedApp.swf");

                         }

                        

                    ]]>

               </fx:Script>

               <s:SWFLoader id="swfLoader" width="100%" height="100%"/>

              

          </s:WindowedApplication>

           

           

          MyLoadedApp is not created with air SDK at all - it is just flash.display.Sprite movie clip created with any compatible Flash compiler:

           

           

          package

          {

               import flash.display.Sprite;

               import flash.events.*;

               import flash.text.TextField;

               import flash.utils.getDefinitionByName;

              

               public class MyLoadedApp extends Sprite

               {

                    public function MyLoadedApp()

                    {

                         addEventListener(Event.ADDED_TO_STAGE, initApp);

                    };

                    //

                    protected function initApp(addedToStageEvent:Event):void

                    {

                         removeEventListener(Event.ADDED_TO_STAGE, initApp);

                         var clazz:Class = flash.utils.getDefinitionByName("flash.filesystem.File") as Class;

                         if(clazz)

                         {

                              // we could read documents directory (File.documentsDirectory) using class access

                              var directory:Object = clazz.documentsDirectory;

                              try

                              {

                                   // and we could show browse dialog (now we are using instance - not class)

                                   directory.addEventListener(Event.SELECT, directorySelectedHandler);

                                   directory.browseForDirectory("Please select directory");

                              } catch (error:Error)

                              {

                                   // handle error

                                   var errorText:TextField = new TextField();

                                   errorText.autoSize = "left";

                                   errorText.text = error.message;

                                   addChild(errorText);

                              };

                         };

                         //

                        

                    };

                    //

                    protected function directorySelectedHandler(selectEvent:Event):void

                    {

                         var directory:Object = selectEvent.target;

                         var files:Array = directory.getDirectoryListing();

                         var resultsTextField:TextField = new TextField();

                         resultsTextField.autoSize = "left";

                         resultsTextField.multiline = true;

                         resultsTextField.appendText(selectEvent.type+":\n");

                         for(var i:uint = 0; i < files.length; i++)

                         {

                              resultsTextField.appendText(files[i].name+"\n");

                         };

                         addChild(resultsTextField);

                    }

               }

          }

           

           

           

          This is not safe at all and loaded module can do everything I think (we talk about local modules from app storage, like in Zinc, right?)

           

          kind regards,

          Peter

          • 2. Re: Accessing AIR API from loaded SWFs
            Calwen Level 1

            Thanks Peter, yes using getDefinitionByName would be a way to do it but it is a bit heavy as I am coding quite a large app.

             

            Am I correct to suspect that only the SWF that is published as an AIR file is allowed to compile AIR classes ? I guess that it would make sense in term of security to prevent externally loaded SWF to trigger malicious code using the AIR API but it is not very flexible.

             

            I find it very frustrating as the only easy way I have found for the loaded SWFs to interact with the AIR API is to bubble events up to the AIR container and to call methods directly on that container, not very flexible.

             

            What I cannot get my head around is how to develop a modular application using AIR if the loaded SWFs cannot access the AIR API directly but only through the AIR container.

             

            Calwen

            • 3. Re: Accessing AIR API from loaded SWFs
              Calwen Level 1

              All sorted it was a false problem. I just added airglobal.swc to the library path in Flash CS5 and made the library as EXTERNAL it compile fine now, phew!