How to determine if an application is running as a

2019-05-09 16:13发布

问题:

How can I find out what the current application type is? i.e. whether it's running on a mobile device or as a desktop Air application?

I've tried this:

if(FlexGlobals.topLevelApplicatoin as WindowedApplication)
 //desktop

However the mobile Version can't find the WindowedApplication class.

How do I tell the difference?

回答1:

ok this works:

public static function isAndroid():Boolean
{
    return (Capabilities.version.substr(0,3) == "AND");
}
public static function isIOS():Boolean
{
    return (Capabilities.version.substr(0,3) == "IOS");
}
 ... //is Blackberry

public static function isMobile():Boolean
{
    return (isAndroid() || isIOS()); // || isBlackberry()
}


回答2:

You can use Capabilities.OS; however heed this warning from the docs:

Do not use Capabilities.os or Capabilities.manufacturer to determine a capability based on the operating system. Basing a capability on the operating system is a bad idea, since it can lead to problems if an application does not consider all potential target operating systems. Instead, use the property corresponding to the capability for which you are testing.



回答3:

If you need to know whether you are running on mobile or desktop you should check for Capabilities.cpuArchitecture:

if(Capabilities.cpuArchitecture=="ARM") {

}



回答4:

I'm not sure, But we can not convert FlexGlobals.topLevelApplicatinn into WindowedApplication in Mobile Application.

So, Mobile Applications can be from the following types:

1.TabbedViewNavigatorApplication - for Tabbed View Navigation Application

2.viewnavigatorapplication - view based navigation application

So as your application type you should try from above two options for conversion....



回答5:

This test will work from a mobile app without needing to test for specific OS names (like Capabilities.os or Capabilities.version). It has the advantage of working consistently when debugging a mobile app on the desktop as well where Capabilities.os may not give you the answer you want:

import flash.utils.getDefinitionByName;
...

var hasWindowedApp:Boolean = false;
try
{
    hasWindowedApp = getDefinitionByName("spark.components.WindowedApplication") != null;
}
catch (error:ReferenceError)
{
}

if (!hasWindowedApp)
{
    try
    {
        hasWindowedApp = getDefinitionByName("mx.core.WindowedApplication") != null;
    }
    catch (error:ReferenceError)
    {
    }
}


回答6:

This is the class I use for determine which os is it and if it's Mobile or not, of course this only covers Windows, Linux Android and iOS:

package com.fw3dlogical.utils {

    import flash.system.Capabilities;

    /**
     * Platform
     * @author Juan Fernando Velez
     */
    public class Platform {

            public static function get isWin():Boolean {
                return (Capabilities.version.indexOf("WIN") != -1);
            }

            public static function get isLinux():Boolean {
                return (Capabilities.version.indexOf("LNX") != -1);
            }

            public static function get isAndroid():Boolean {
                return (Capabilities.version.indexOf("AND") != -1);
            }

            public static function get isiOS():Boolean {
                return (Capabilities.version.indexOf("IOS") != -1);
            }

            public static function isMobile():Boolean {
                return (isAndroid() || isiOS());
            }

        }
    }
}


回答7:

C# Function to check IOS(iPad, iPhone)

    public bool isIOS()
    {
        HttpContext context = HttpContext.Current;

        if (context.Request.ServerVariables["HTTP_USER_AGENT"] != null)
        {
            foreach (string s in new[] { "iPad", "iphone" })
            {
                if (context.Request.ServerVariables["HTTP_USER_AGENT"].ToLower().Contains(s.ToLower()))
                {
                    return true;
                }
            }
        }

        return false;
    }