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?
Go To Xamarin Studio. Click New Solution—> select Multiplatform—>select App--> Choose single View App. Native (iOS,Android). Afterwards, click Next.
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.
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.
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) { }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With