기기별로 유일한 키를 사용하고 싶을때 아래 코드를 이용하면

기기별로 유일한 키를 얻어 사용할수있다.

앱을 삭제했다 다시깔아도 동일한 키가 추출된다.

 

PCL

namespace Project
{
    public interface IDevice
    {
        string GetIdentifier();
    }
}

 

Android

[assembly: Xamarin.Forms.Dependency(typeof(AndroidDevice))]
namespace Project.Droid
{
    public class AndroidDevice : IDevice
    {
        public string GetIdentifier()
        {
            return Secure.GetString(Forms.Context.ContentResolver, Secure.AndroidId);
        }
    }
}

 

iOS

[assembly: Xamarin.Forms.Dependency(typeof(IOSDevice))]
namespace Project.iOS
{
    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;
        }
    }
}

+ Recent posts

티스토리 툴바