一份代码完美运行于两种环境 Single AS Code Source that Runs Perfectly in Browser and AIR

In Flex, you never need two different sources to build your application in browser and in AIR. For example:



  <![CDATA[
    import flash.utils.getDefinitionByName;
    import flash.filesystem.File;
    import com.insprise.common.SystemUtils;

  public function init() {
    mylabel.text = "User dir: " + (SystemUtils.isAirEnvironment() ? getUserDir() : "");
  }

  public static function getUserDir():String {
    return File.userDirectory.toString();
  }
  ]]>

  

The above code will run perfect in both Browser and AIR. One rule to remember:

No AIR-only property/function should be executed when in browser. It’s OK that your class imports AIR-only classes as long as AIR-only code is not executed.