views:

700

answers:

3

Hello,

I have an swc which needs to operate slightly differently depending on whether it is being hosted by AIR or not. I've seen two suggestions on the internet:

  • Test Application.application for WindowedApplication.
  • Test Security.sandboxType for Security.APPLICATION.

However, these don't seem to work in my .swc as the compiler can't find WindowedApplication or Security.APPLICATION. My library doesn't need any other AIR features so I assume it's not being linked to the AIR libraries, and I assume shouldn't be in order that it continues to work in flash player.

Any suggestions?

+1  A: 

I would try flash.utils.getDefinitionByName() which will allow you to pass one of the AIR API classes as a String. This should throw an error which you can catch in a Flex environment or be successful in an AIR environment.

cliff.meyers
A: 

I think your best bet is actually using getQualifiedSuperclassName, if only because it does not force you to throw an error. Just pass any DisplayObject (which has been added to the stage) to the following function:

import flash.utils.getQualifiedSuperclassName;
public function isAirApp( obj:DisplayObject ):Boolean
{
     while( getQualifiedSuperclassName( obj.parent ) !=
            "mx.managers::SystemManager" )
    {
         obj = obj.parent;
    }
    return ( getQualifiedSuperclassName( obj ) ==
             "mx.core::WindowedApplication" );
}

Christopher W. Allen-Poole
This assumes that your application inherits directly from WindowedApplication / Application, which is quite often not the case.
Richard Szalay
+9  A: 

You can use the flash.system.Capabilities object to find out if you running under AIR.

var isAir : Boolean = (Capabilities.playerType == "Desktop");
var isFlashPlayer : Boolean = (Capabilities.playerType == "StandAlone");
var isBrowser : Boolean = (Capabilities.playerType == "ActiveX" || Capabilities.playerType == "PlugIn");
var isOther : Boolean = (Capabilities.playerType == "External");
Richard Szalay