Figuring out your if your app is run on Phone, Tablet or Desktop

One would think that it’s easy to figure out the answer to header above. If you’ve ever tried that, I think you most likely have become extremely frustrated when you realized that there are just too many combinations and it becomes quite impossible to make a difference between the devices with the provided APIs. For example Surface is a tablet running Desktop SKU while under 8″ screen tablets run Mobile SKU which is same SKU which is on the phones. For IoT there’s IoT Core and IoT Enterprise, which is actually Windows 10 Enterprise with additional IoT license.

In general you should always design for Universal which adapts to available screen space and hardware/capabilities but sometimes you just need to know (for example for analytic reasons) and that’s why I have created this little snippet. It should cover the most cases, but do let me know if the logic fails to identify something and I try to include that as well.

I threw in my best guess for Continuum and Iot as well, after some Twitter discussions with @dotMorten, @ScottIsAFool, @gcaughey and @mahoekst

public enum Device
{
    Phone,
    Tablet,
    Desktop,
    Xbox,
    Iot,
    Continuum
};
...
if (Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Mobile")
{
    if (ApiInformation.IsApiContractPresent("Windows.Phone.PhoneContract", 1))
    {
        Windows.Devices.Input.KeyboardCapabilities keyboard = new Windows.Devices.Input.KeyboardCapabilities();
        if (keyboard.KeyboardPresent > 0)
        {
            runningDevice = Device.Continuum;
        }
        else
        {
            runningDevice = Device.Phone;
        }
    }
    else
    {
        runningDevice = Device.Tablet;
    }
}
else if (Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Desktop")
{
    Windows.Devices.Input.KeyboardCapabilities keyboard = new Windows.Devices.Input.KeyboardCapabilities();
    if (keyboard.KeyboardPresent > 0)
    {
        runningDevice = Device.Desktop;
    }
    else
    {
        runningDevice = Device.Tablet;
    }
}
else if (Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Xbox")
{
    runningDevice = Device.Xbox;
}
else if (Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.IoT")
{
    runningDevice = Device.Iot;
}

// Couldn't figure out, let's assume it's desktop (safest bet)
runningDevice = Device.Desktop;

About Jani Nevalainen

Windows developer who's been working on Microsoft Platforms since 1996. Windows Phone dev MVP 2013, Windows Development Platform MVP 2014. Currently working as Technical Evangelist for Microsoft Finland Developer Experience team.
This entry was posted in Universal Apps, UWP, Windows 10, Windows Phone. Bookmark the permalink.

One Response to Figuring out your if your app is run on Phone, Tablet or Desktop

  1. Pingback: Windows 10 UWP Uygulamas?n?n Phone, Tablet Veya Desktop Cihazda ├žal??t???n? Bulmak | Engin Polat'?n Windows 8 , Windows Phone 8 Ve C# I├žerikli Programc?l?k Sitesi

Leave a Reply

Your email address will not be published. Required fields are marked *