Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the current device model on Xamarin Forms?

I wrote a logger class that emails me when there's an exception on my application, but I'd like to know what the model of device is that threw the exception. For example:

Logger.LogError(App.ApplicationName, Device.OS.ToString(), "Error getting passenger ancillary transactions!", Settings.CurrentUsername, ex.ToString());

Sends an email of the exception with the application's name and the fact that the device is "Android" or "iOS", but nothing more specific than that. In Visual Studio, it says which device I'm about to debug on (like "iPod Touch" or "LG LS991"). Is there a way to access this information? I can't find anything in the Xamarin documentation.

like image 793
aufty Avatar asked Apr 18 '16 16:04

aufty


People also ask

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.

Is Xamarin being discontinued?

Xamarin support will end on May 1, 2024 for all Xamarin SDKs. Android 13 and Xcode 14 SDKs (iOS and iPadOS 16, macOS 13) will be the final versions Xamarin will target.

How do I know what version of Xamarin I have?

You can find the version of your Xamarin plug-ins for Visual Studio by going to "Help" -> "About Microsoft Visual Studio" in VS, and scrolling down to the Xamarin plug-ins.


2 Answers

Xamarin.Plugins by jamesmontemagno work cross-platform within Xamarin.Forms:

You would be looking for the DeviceInfo plugin:

https://github.com/jamesmontemagno/Xamarin.Plugins/tree/master/DeviceInfo

Nuget search: 'Device Info Plugin for Xamarin and Windows'

Device Model

/// <summary>
/// Get the model of the device
/// </summary>
string Model { get; }

Version

/// <summary>
/// Get the version of the Operating System
/// </summary>
string Version { get; }
Returns the specific version number of the OS such as:

iOS: 8.1
Android: 4.4.4
Windows Phone: 8.10.14219.0
WinRT: always 8.1 until there is a work around

Platform

/// <summary>
/// Get the platform of the device
/// </summary>

Platform { get; }

Returns the Platform Enum of:

public enum Platform
{
  Android,
  iOS,
  WindowsPhone,
  Windows
}
like image 137
SushiHangover Avatar answered Sep 22 '22 17:09

SushiHangover


Just to complete SushiHangover's answer: On iOS, Model property does not contain full model name(e.g. you cannot distinguish iPhone 3 and iPhone 4).

Fortunately, here is some code, that extracts it for you.

like image 34
Flame239 Avatar answered Sep 22 '22 17:09

Flame239