Categories
Windows

Determine if running on Windows Phone from a UAP application

There is new metadata query support in Windows 10 to allow your code to react at runtime based on which APIs are available. For example if running on a phone device you may want to start a call, and offer alternative actions on a desktop PC. The correct way to check the availability is to use the ApiInformation class which has a number of methods which take the string name of the type or method and return a Boolean. Without delving into the question of whether a string is the correct approach lets look at an alternative issue.

You want to determine the device type you are running on – say Windows, Windows Phone, Xbox etc to change the type of content you display. You want this high level information not just to alter screen layout or use a specific API. Each Extension SDK provides the device specific APIs for the target devices. Currently the preview tools ship with Windows Desktop Extension SDK and Windows Mobile Extension SDK. If you look at the Object Browser in Visual Studio you can see that adding each of these adds additional root level entries (like a new DLL) and each named after a Contract which is a type contained within it alongside the associated APIs. So you want to quickly determine if you are running on a “Windows Phone” device you could use the following code:-

bool isPhone = ApiInformation.IsApiContractPresent("Windows.Phone.PhoneContract",1);

It’s not currently clear exactly which contracts will be present on which devices – the APIs provided by the Windows.Phone.PhoneContract lib are all familiar APIs present from previous versions of Windows Phone. Since “Windows Mobile” will also be the SKU used for small tablets it isn’t clear if these will also implement this contract or not. If no extension SDKs are added all UAP apps will support Windows.Foundation.FoundationContract and Windows.Foundation.UniversalApiContract.

Although not a part of the current preview tools one can expect a similar set of contracts for the other flavours of Windows – Xbox, IoT etc

By Peter Foot

Microsoft Windows Development MVP