Hello,
How can I get the device's unique ID in MAUI?
For iOS, we cannot get the device's unique ID. iOS puts serious restrictions on unique persistent identifiers because of privacy concerns
For Android, we can use Android.Provider.Settings.Secure
to get it.
public class GetDeviceInfo {
public string GetDeviceID() {
#if ANDROID
var context = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity;
string id = Secure.GetString(context.ContentResolver, Secure.AndroidId);
return id;
#endif
return "-1";
}
}
Here is recommended way.
If you are attempting to do is ensure that when an application is installed, you can get some unique identifier for that installation. If the app is transferred to a new phone, then the identifier is the same, but if it is un-installed then a new identifier is generated. You can generate GUID
, then save or get it in your Preferences like following code. This will ensure a unique identifier for the installed application.
public App()
{
InitializeComponent();
var id = Preferences.Get("my_id", string.Empty);
if (string.IsNullOrWhiteSpace(id)) {
id = System.Guid.NewGuid().ToString();
Preferences.Set("my_id", id);
}
MainPage = new AppShell();
}
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.