Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 405
Askolein Avatar asked Sep 02 '13 10:09

Askolein


People also ask

How do I get OS version in xamarin?

Go To Xamarin Studio. Click New Solution—> select Multiplatform—>select App--> Choose single View App. Native (iOS,Android). Afterwards, click Next.

How do I check my Android version xamarin?

For version check Help/About and then click Show Details. For Visual Studio updates go to Tools/Options and then Xamarin from the left tree. Below you'll find Xamarin. Android updates.

What is xamarin essentials?

Xamarin. Essentials provides a single cross-platform API that works with any iOS, Android, or UWP application that can be accessed from shared code no matter how the user interface is created. See the platform & feature support guide for more information on supported operating systems.


2 Answers

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) {  } 
like image 185
maulik sakhare Avatar answered Sep 18 '22 20:09

maulik sakhare


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.

like image 38
jzeferino Avatar answered Sep 19 '22 20:09

jzeferino