Saturday, February 17, 2024

Device ID from iOS and Android platforms using .Net MAUI

In most of the scenarios app developers require unique device Ids to identify the duplicasy of their apps, or to encrypt their data with salt key as device Ids. So to get these device Id's we have to write a platform specific code in .NET Maui. In Xamarin.Forms we rely on Dependency Services to write the platform specific code, but in .NET Maui this can be achieved by creating partial classes. Here I'll explain step by step procedure for obtaining device Id's from partial class.

Cross platform partial class

Create a partial class and partial method, that will be implemented in platform specific partial classes.

 

GetDeviceInfo - Cross platform

With the GetDeviceID method we should obtain device Id's from each platform. 
Note: all the partial classes should have the same namespace, even though they are created in different folders, their namespace should match each other.

Android partial class 

Navigate to Platform\Android folder in the project solution and create another partial class.

iOS partial class 

Navigate to Platform\iOS folder in the project solution and create another partial class.

Here is how we call the platform specific partial method in our MainPage.xaml.cs file.

 
Finally the outcome of this implementation is here:
iOS Device id
Android Device Id




















No comments:

Post a Comment

SSO login using Launcher in .NET MAUI

  Some of the scenarios require to open the external/system browser to perform oAuth 2.0 login and then receive the auth code via redirect U...