Getting Unique Device ID for iOS, Android and Windows with Xamarin

Over a year ago, I wrote a quick tip on getting device Unique ID for Windows and Windows Phone (8 & 7). Lately, at RedBit, we have been doing quite a bit of iOS, Android and Windows projects using Xamarin and recently put two articles up on our learnings, Cross Platform Animations using Xamarin Forms and GZip Compression with Xamarin Forms. For different reasons, we needed to use a unique ID to identify devices and getting the device ID on each platform is obviously different.

For iOS you can use the UIDevice.CurrentDevice.IdentifierForVendor which can be called from a Xamarin iOS or Xamarin Forms app.

UIKit.UIDevice.CurrentDevice.IdentifierForVendor.AsString();

For Android, again it’s a different API but note, that you need API Level 9 or higher for this to work. It is called as follows from Xamarin Android or Xamarin Forms

Android.OS.Build.Serial;

For Windows you can reference this post I wrote a while back Getting Device ID on Windows & Windows Phone.

At RedBit we have an internal library that we use and here is an excerpt of our RedBit.Mobile.Hardware class

public class Hardware
{
    private static Hardware _Default;
    public static Hardware Default
    {
        get
        {
            return _Default ?? (_Default = new Hardware());
        }
    }

    ///

    /// Gets a device unique identifier
    ///

public string DeviceId { get { return GetDeviceIdInternal(); } } ///
/// Gets a device unique identifier depending on the platform ///

/// string representing the unique id private string GetDeviceIdInternal() { var id = default(string); #if __IOS__ id = UIKit.UIDevice.CurrentDevice.IdentifierForVendor.AsString(); #elif WINDOWS_PHONE id = Windows.Phone.System.Analytics.HostInformation.PublisherHostId; #else id = Android.OS.Build.Serial; #endif return id; } }

I’ve put a sample up on GitHub that outputs the device ID for iOS, Android and Windows. Here is the output on iOS.

deviceId-ios

As always, comments welcome!


Warning: count(): Parameter must be an array or an object that implements Countable in /home/usnbis1maldq/domains/markarteaga.com/html/wp-includes/class-wp-comment-query.php on line 405

Leave a Reply