如何在Xamarin Forms中使用C#获取Android和iOS设备的唯一标识?我正在使用Azure Notification Hub发送通知。我正在参考这篇博客。但是,在Android中我找不到相关的“设置”。
如何在Xamarin Forms中使用C#获取Android和iOS设备的唯一标识?我正在使用Azure Notification Hub发送通知。我正在参考这篇博客。但是,在Android中我找不到相关的“设置”。
Android.Provider.Settings.Secure.GetString(Android.App.Application.Context.ContentResolver, Android.Provider.Settings.Secure.AndroidId);
对于 iOS,请查看您的博客文章。或者可将 IdentifierForVendor
保存在 AppDelegate
中,并在 IOSDevice
类中返回该值(使用博客文章中的名称)。使用 UIDevice.CurrentDevice.IdentifierForVendor.ToString()
获取 iOS 设备的 ID。Android.Provider.Settings.Secure.GetString(Android.App.Application.Context.ContentResolver, Android.Provider.Settings.Secure.AndroidId);
,但其中一个应用程序返回了不同的设备 ID,而其他4个应用程序返回相同的设备 ID。我在同一台设备上测试了这5个应用程序。 - Deepak yogi这里详细描述了如何为移动应用生成唯一设备ID。但实际上,您不需要像这样尝试从每个设备获取一个ID。只需创建一个Guid并保存到设备上即可运行。Xamarin拥有Preferences功能,可将值持久化在设备上。
您可以像我下面所做的那样创建一个guid并将其保存到Preferences中:
var deviceId = Preferences.Get("my_deviceId", string.Empty);
if(string.IsNullOrWhitespace(deviceId))
{
deviceId = System.Guid.NewGuid().ToString();
Preferences.Set("my_deviceId", deviceId);
}
采用这种方法的好处是在您将应用程序转移到另一个设备后,您仍将拥有相同的ID;但是,如果您卸载并重新安装,则会获得新的ID。对于从设备获取ID的其他情况,在将应用程序转移到另一个设备时,它将发生变化。
对于您想从设备获取ID的其他情况:
iOS:IdentifierForDevice
public string Id => UIDevice.CurrentDevice.IdentifierForVendor.AsString();
Android: Serial、getSerial和AndroidId
string id = string.Empty;
public string Id
{
get
{
if (!string.IsNullOrWhiteSpace(id))
return id;
id = Android.OS.Build.Serial;
if (string.IsNullOrWhiteSpace(id) || id == Build.Unknown || id == "0")
{
try
{
var context = Android.App.Application.Context;
id = Secure.GetString(context.ContentResolver, Secure.AndroidId);
}
catch (Exception ex)
{
Android.Util.Log.Warn("DeviceInfo", "Unable to get id: " + ex.ToString());
}
}
return id;
}
}
UWP:GetPackageSpecificToken或GetSystemIdForPublisher
string id = null;
public string Id
{
get
{
if (id != null)
return id;
try
{
if (ApiInformation.IsTypePresent("Windows.System.Profile.SystemIdentification"))
{
var systemId = SystemIdentification.GetSystemIdForPublisher();
// Make sure this device can generate the IDs
if (systemId.Source != SystemIdentificationSource.None)
{
// The Id property has a buffer with the unique ID
var hardwareId = systemId.Id;
var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(hardwareId);
var bytes = new byte[hardwareId.Length];
dataReader.ReadBytes(bytes);
id = Convert.ToBase64String(bytes);
}
}
if (id == null && ApiInformation.IsTypePresent("Windows.System.Profile.HardwareIdentification"))
{
var token = HardwareIdentification.GetPackageSpecificToken(null);
var hardwareId = token.Id;
var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(hardwareId);
var bytes = new byte[hardwareId.Length];
dataReader.ReadBytes(bytes);
id = Convert.ToBase64String(bytes);
}
if (id == null)
{
id = "unsupported";
}
}
catch (Exception)
{
id = "unsupported";
}
return id;
}
}
有一个由James Montemagno提供的插件。
在NuGet Packages中搜索Xam.Plugin.DeviceInf即可找到。Github链接:https://github.com/jamesmontemagno/DeviceInfoPlugin