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;
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

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