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.
| iOS Device id |
![]() |
| Android Device Id |

No comments:
Post a Comment