Hello,
Welcome to our Microsoft Q&A platform!
Is there an easy way to obtain a unique id via Xamarin Forms
To get the idenifier for device, try to achieve the function on the native platform using DependencyService.
//1.create an interface to define the method
public interface IGetDeviceInfo
{
string GetDeviceID();
}
//2.implement the service on the android platform
[assembly: Xamarin.Forms.Dependency(typeof(GetInfoImplement))]
namespace TestApplication_4.Droid
{
public class GetInfoImplement : IGetDeviceInfo
{
string IGetDeviceInfo.GetDeviceID()
{
var context = Android.App.Application.Context;
string id = Android.Provider.Settings.Secure.GetString(context.ContentResolver, Secure.AndroidId);
return id
}
}
}
//3.consume the DependencyService command in the shared project
DependencyService.Get<IGetDeviceInfo>().GetDeviceID();
For the iOS platform, you could use the following method.
string id = UIDevice.CurrentDevice.IdentifierForVendor.AsString();
Best Regards,
Jarvan Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.