35

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?

Stephane Delcroix
  • 15,420
  • 5
  • 52
  • 82
Askolein
  • 2,776
  • 2
  • 23
  • 36
  • May I ask what you want to do with that info? – Daniel Hilgarth Sep 02 '13 at 10:27
  • possible duplicate of [How to retrieve the android sdk version?](http://stackoverflow.com/questions/1882883/how-to-retrieve-the-android-sdk-version) – Andrei Sep 02 '13 at 10:27
  • @Daniel: my server (providing data and service) needs to know `who` is making the request (logging, push notifications, some specific data for the UI...) – Askolein Sep 02 '13 at 10:28
  • 2
    @Andrei: nope. The question you're relating to is about Android only SDK version. I need to have a discriminant between Android/iOS. – Askolein Sep 02 '13 at 10:30

3 Answers3

90

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)
{

}
maulik sakhare
  • 1,444
  • 10
  • 17
  • Just to enhance this answer, one can also use Xamarin.Essentials with the DeviceInfo API. DeviceInfo.Platform will also return the platform, e.g. 'Android'. – jfmg Apr 30 '21 at 13:01
6

I'm using DeviceInfo plugin. It supports Android, iOs, Windows Phone Silverlight, Windows Phone RT, Windows Store RT, Windows 10 UWP and it works perfectly.

It can be used in PCL like this (you must add the plugin to the platform projects and to PCL):

var platform = CrossDeviceInfo.Current.Platform;
    switch(platform){
        case Platform.iOS:
            // iOS
            break;
        case Platform.Android:
            // Android
            break; 
    }

This kind of code isn't a good practice in PCL, its better to use Dependency Injection (this plugin use that) to customize platform specific behaviour.

jzeferino
  • 7,430
  • 1
  • 33
  • 57
4

Using reflection, try to retrieve the value of the Monotouch.Version property (and the equivalent for MfA: Android.OS.Build.VERSION). One of the calls will fail, the other should succeed; that's why you have to use reflection. That's for a real runtime check.

But as your app is compiled twice, you can fix that value at compile time.

#if MONOTOUCH
    var platform = "iOS"
#else
    var platform = "Android"
#endif
Stephane Delcroix
  • 15,420
  • 5
  • 52
  • 82
  • thanks for those solutions. I hoped another one exists. Those imply you to handle any new platform (aka windows) by hand. Not a big issue indeed but it seems strange to me that Xamarin does not provide suche service in its API. – Askolein Sep 02 '13 at 13:46
  • Im using Xamarin iOS 9.6.2, Xamarin Android 6.0.4.0 and Xamarin Studio 5.10.3. The MONOTOUCH symbol don't exist. That #ifdef always go to the else. Yes im setting the iOS project as default. Any alternative? – jzeferino May 18 '16 at 08:39