Search iPad generation in Monotouch

I need to determine which iPad version for my application is working before setting up the openGl surface.

If it works on an old iPad 1, it is too slow to enable anti-aliasing, whereas on iPad 2 + 3 there should be no performance issues, so I need to detect this first.

Any ideas on how to detect iPad generation using Monotouch?

+3


source to share


2 answers


Thanks to Joachim, your hint led me to the following solution, which I tested on physical iPads 1 to 3 generations and should also detect other Apple devices:



public enum HardwareVersion
{
        iPhone2G,
        iPhone3G,
        iPhone3Gs,
        iPhone4,
        iPod1G,
        iPod2G,
        iPod3G,
        Simulator,
        iPad1G,
        iPad2G,
        iPad3G,
        Unknown
}


[DllImport(MonoTouch.Constants.SystemLibrary)]
static internal extern int sysctlbyname ([MarshalAs(UnmanagedType.LPStr)] string property, IntPtr output, IntPtr oldLen, IntPtr newp, uint newlen);

public static HardwareVersion getHardwareVersion()
    {
        string HardwareProperty = "hw.machine";

        // get the length of the string that will be returned
        var pLen = Marshal.AllocHGlobal (sizeof(int));
        sysctlbyname (HardwareProperty, IntPtr.Zero, pLen, IntPtr.Zero, 0);

        var length = Marshal.ReadInt32 (pLen);

        // check to see if we got a length
        if (length == 0) {
                Marshal.FreeHGlobal (pLen);
                return HardwareVersion.Unknown;
        }

        // get the hardware string
        var pStr = Marshal.AllocHGlobal (length);
        sysctlbyname (HardwareProperty, pStr, pLen, IntPtr.Zero, 0);

        // convert the native string into a C# string
        var hardwareStr = Marshal.PtrToStringAnsi (pStr);
        var ret = HardwareVersion.Unknown;

        // determine which hardware we are running
        if (hardwareStr == "iPhone1,1")
                ret = HardwareVersion.iPhone2G; 
        else if (hardwareStr == "iPhone1,2")
                ret = HardwareVersion.iPhone3G; 
        else if (hardwareStr == "iPhone2,1")
                ret = HardwareVersion.iPhone3Gs; 
        else if (hardwareStr == "iPhone3,1")
                ret = HardwareVersion.iPhone4; 
        else if (hardwareStr == "iPod1,1")
                ret = HardwareVersion.iPod1G; 
        else if (hardwareStr == "iPod2,1")
                ret = HardwareVersion.iPod2G; 
        else if (hardwareStr == "iPod3,1")
                ret = HardwareVersion.iPod3G; 
        else if (hardwareStr == "iPad1,1")
                ret = HardwareVersion.iPad1G; 
        else if (hardwareStr == "iPad2,1")
                ret = HardwareVersion.iPad2G; 
        else if (hardwareStr == "iPad3,1")
                ret = HardwareVersion.iPad3G; 
        else if (hardwareStr == "i386" || hardwareStr == "x86_64"  || hardwareStr == "x86_32" )
                ret = HardwareVersion.Simulator;

        // cleanup
        Marshal.FreeHGlobal (pLen);
        Marshal.FreeHGlobal (pStr);

        return ret;
}

      

+5


source


You can find the hardware version using P / Invoke for sysctlbyname

.



It's a bit tricky, so I would recommend you use the code in the Xamarin Wiki and extend it with iPad2 (iPad2,1) and above yourself. Maybe update the wiki with your changes as well :)

+3


source







All Articles