Xamarin.Forms device unique ID...
As in a lot of applications, you will probably need to get a unique identifier to identify a unique user.
And it becomes a little more complicated when you have to deal with different platforms like Android, iOS and Windows Phone...
Here is a simple solution that you can implement in Xamarin Forms that I found on this blog:
http://codeworks.it/blog/?p=260
First define a service that you will use in your Xamarin Forms Application:
And here are the implementations for Android / Windows Phone and the most complicated, iOS platform:
Original post:
http://codeworks.it/blog/?p=260
And it becomes a little more complicated when you have to deal with different platforms like Android, iOS and Windows Phone...
Here is a simple solution that you can implement in Xamarin Forms that I found on this blog:
http://codeworks.it/blog/?p=260
First define a service that you will use in your Xamarin Forms Application:
// Your service interface definition public interface IDevice { string GetIdentifier(); } // How you get your service in your app IDevice device = DependencyService.Get(); string deviceIdentifier = device.GetIdentifier();
And here are the implementations for Android / Windows Phone and the most complicated, iOS platform:
// WINDOWS PHONE [assembly: Xamarin.Forms.Dependency(typeof(WinPhoneDevice))] namespace XFUniqueIdentifier.WinPhone { public class WinPhoneDevice : IDevice { public string GetIdentifier() { byte[] myDeviceId = (byte[])Microsoft.Phone.Info.DeviceExtendedProperties.GetValue("DeviceUniqueId"); return Convert.ToBase64String(myDeviceId); } } }
// ANDROID [assembly: Xamarin.Forms.Dependency(typeof(AndroidDevice))] namespace XFUniqueIdentifier.Droid { public class AndroidDevice : IDevice { public string GetIdentifier() { return Settings.Secure.GetString(Forms.Context.ContentResolver, Settings.Secure.AndroidId); } } }
// iOS // [assembly: Xamarin.Forms.Dependency(typeof(IOSDevice))] namespace XFUniqueIdentifier.iOS { using System.IO; using System.Runtime.InteropServices; public class IOSDevice : IDevice { [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")] private static extern uint IOServiceGetMatchingService(uint masterPort, IntPtr matching); [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")] private static extern IntPtr IOServiceMatching(string s); [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")] private static extern IntPtr IORegistryEntryCreateCFProperty(uint entry, IntPtr key, IntPtr allocator, uint options); [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")] private static extern int IOObjectRelease(uint o); public string GetIdentifier() { string serial = string.Empty; uint platformExpert = IOServiceGetMatchingService(0, IOServiceMatching("IOPlatformExpertDevice")); if (platformExpert != 0) { NSString key = (NSString) "IOPlatformSerialNumber"; IntPtr serialNumber = IORegistryEntryCreateCFProperty(platformExpert, key.Handle, IntPtr.Zero, 0); if (serialNumber != IntPtr.Zero) { serial = NSString.FromHandle(serialNumber); } IOObjectRelease(platformExpert); } return serial; } }
Original post:
http://codeworks.it/blog/?p=260
Comments
Post a Comment