Get Device Unqiue ID

Jassim Al Rahma 1,591 Reputation points
2022-10-24T16:28:37.437+00:00

Hi,

How can I get the device's unique ID in MAUI?

Thanks,
Jassim

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,831 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 78,586 Reputation points Microsoft Vendor
    2022-10-25T02:57:19.297+00:00

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.