How to know the current OS / platform of the executing code (Android / iOS)

Askolein picture Askolein · Sep 2, 2013 · Viewed 25.9k times · Source

Using Xamarin.Android and Xamarin.iOS, I need to now the current OS in a shared code section. Be it an enum, an int or a string, it doesn't matter.

I tried this:

System.Environment.OSVersion

Which is always "Unix" with some kernel version informations.

There are info on Android for example like

Android.OS.Build.VERSION

But it needs to be in the Android specific code section. Is there any way of knowing the current OS in a common library?

Answer

maulik sakhare picture maulik sakhare · Jun 20, 2017

You can also try this and its probably the best solution I found :

if(Device.RuntimePlatform == Device.iOS)
{
    //iOS stuff
}
else if(Device.RuntimePlatform == Device.Android)
{

}