I'd like to get precise device model (e.g. iPad 3) in MonoTouch.
I've seen it done in Objective C but I haven't figured out the correct P/Invokes yet.
Asked
Active
Viewed 2,177 times
1

Community
- 1
- 1

Dan Abramov
- 264,556
- 84
- 409
- 511
2 Answers
5
I took this from a few places and changed it to what I needed, so I can't take full credit for it. It really stinks to have an enum that has to be updated, but oh well.
But this is what I have:
public static class DeviceHelper
{
public const string HardwareProperty = "hw.machine";
public static readonly string PLATFORM;
public static bool IsTall
{
get
{
return UIDevice.CurrentDevice.UserInterfaceIdiom
== UIUserInterfaceIdiom.Phone
&& UIScreen.MainScreen.Bounds.Height
* UIScreen.MainScreen.Scale >= 1136;
}
}
[DllImport(MonoTouch.Constants.SystemLibrary)]
static internal extern int sysctlbyname([MarshalAs(UnmanagedType.LPStr)] string property, IntPtr output, IntPtr oldLen, IntPtr newp, uint newlen);
static DeviceHelper ()
{
var pLen = Marshal.AllocHGlobal(sizeof(int));
sysctlbyname(HardwareProperty, IntPtr.Zero, pLen, IntPtr.Zero, 0);
var length = Marshal.ReadInt32(pLen);
var pStr = Marshal.AllocHGlobal(length);
sysctlbyname(HardwareProperty, pStr, pLen, IntPtr.Zero, 0);
var hardwareStr = Marshal.PtrToStringAnsi(pStr);
var ret = HardwareVersion.Unknown;
if (hardwareStr == "iPhone1,1")
ret = HardwareVersion.iPhone;
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 == "iPhone3,3")
ret = HardwareVersion.VerizoniPhone4;
else if(hardwareStr == "iPhone4,1")
ret = HardwareVersion.iPhone4S;
else if(hardwareStr == "iPhone 5,1" || hardwareStr == "iPhone 5,2")
ret = HardwareVersion.iPhone5;
else if (hardwareStr == "iPad1,1")
ret = HardwareVersion.iPad;
else if (hardwareStr == "iPad2,1")
ret = HardwareVersion.iPad2WIFI;
else if (hardwareStr == "iPad2,2")
ret = HardwareVersion.iPad2GSM;
else if (hardwareStr == "iPad2,3")
ret = HardwareVersion.iPad2CDMA;
else if (hardwareStr == "iPad2,4")
ret = HardwareVersion.iPad2WIFI24;
else if (hardwareStr == "iPad3,1")
ret = HardwareVersion.iPad3WIFI;
else if (hardwareStr == "iPad3,2")
ret = HardwareVersion.iPad3GSM;
else if (hardwareStr == "iPad3,3")
ret = HardwareVersion.iPad3CDMA;
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 == "iPod4,1")
ret = HardwareVersion.iPod4G;
else if (hardwareStr == "i386" || hardwareStr=="x86_64") {
if (UIDevice.CurrentDevice.Model.Contains("iPhone"))
ret = UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale == 960 || UIScreen.MainScreen.Bounds.Width * UIScreen.MainScreen.Scale == 960 ? HardwareVersion.iPhone4Simulator : HardwareVersion.iPhoneSimulator;
else
ret = HardwareVersion.iPadSimulator;
}
if(ret == HardwareVersion.Unknown)
PLATFORM = hardwareStr;
else
PLATFORM = ret.ToString();
}
public enum HardwareVersion {
iPhone,
iPhone3G,
iPhone3GS,
iPhone4,
VerizoniPhone4,
iPhone4S,
iPhone5,
iPod1G,
iPod2G,
iPod3G,
iPod4G,
iPad,
iPad2WIFI,
iPad2WIFI24,
iPad2GSM,
iPad2CDMA,
iPad3WIFI,
iPad3GSM,
iPad3CDMA,
iPhoneSimulator,
iPhone4Simulator,
iPadSimulator,
Unknown
}
}

valdetero
- 4,624
- 1
- 31
- 46
-
Cool, this seems more thought out than my solution (though the core is the same). Also, using enum is good. Thanks! – Dan Abramov Feb 05 '13 at 16:00
-
Hi. Do you know the `MonoTouch.Constants.SystemLibrary` equivalent in latest `Xamarion.iOS`. I can't find that class for a DllImport in my project. Thanks. – Shawinder Sekhon Nov 09 '17 at 19:39
2
I used this answer as a basis.
I'm not sure this is the shortest way, but it works.
[DllImport ("libc", CallingConvention = CallingConvention.Cdecl)]
static extern int sysctlbyname (string name, StringBuilder value, ref IntPtr length, IntPtr newp, IntPtr newlen);
public static string GetDeviceName (this UIDevice device)
{
IntPtr size = (IntPtr) 8;
var sb = new StringBuilder ();
string param = "hw.machine";
sysctlbyname (param, sb, ref size, IntPtr.Zero, (IntPtr) 0);
return sb.ToString ();
}

Community
- 1
- 1

Dan Abramov
- 264,556
- 84
- 409
- 511
-
3You shouldn't be passing 8 as the length, since some identifiers will be longer than that, in which case you get null back for your string. Instead, you should call sysctlbyname while passing a null value first. This will cause size to be updated with the correct length, after which you can call it again to obtain the entire identifier. – Ryan Pendleton Apr 17 '18 at 23:37