Search code examples
uno-platform

Getting device information in class library with Uno Platform


I'm trying to develop a class library for use from Uno Platform applications that can be distributed as a NuGet package. In the code, I need to be able to extract various information about the device currently running the code. I already figured out how to get the screen dimensions but now I'm stuck in getting the current operating system and version. I have seen code like this:

#if __ANDROID__
    os = "android";
#elif __IOS__
    os = "ios";
#endif

But as I understand, this is not supported in class libraries. I have also experimented with the Uno port of Xamarin.Essentials to use the DeviceInfo class. I couldn't get this to work, though.

Any help would be appreciated.


Solution

  • I approach getting the OS info in a different manner. I grab the OS info using the following code:

    public static string getOSVersion()
        {
            string osInfo = Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily.ToString();
            int dotPosition = osInfo.IndexOf('.');
            string shortOsInfo = osInfo.Substring(0, dotPosition);
            return  shortOsInfo + " " + Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamilyVersion.ToString();
        }
    

    If you are on Android- it will return Android 11, on IOS it will return iOS 14.4(Keep in mind the version number will probably be different- just using it for example purposes). I have not run this on WASM, UWP, Linux, or MacOS- but it should work. Hopefully this will be helpful to you!