tomasslavicek

Xamarin.iOS MonoTouch detect specific device iOS iPhone iPod

May 11th, 2013
1,259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.59 KB | None | 0 0
  1. //if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
  2. //    SetTabletGraphics();
  3. //else
  4. //    SetMobileGraphics();
  5.  
  6. using System;
  7. using System.Runtime.InteropServices;
  8. using MonoTouch;
  9. using MonoTouch.UIKit; // Only needed if you use constant instead of hardcoded library name
  10.  
  11. namespace HexaLines_iOS
  12. {
  13.     public static class HardwareInfo // As it works on any Darwin
  14.     {
  15.         public const string HardwareProperty = "hw.machine"; // Change to "hw.model" for getting the model in Mac OS X and not just the CPU model
  16.  
  17.         // Does not include Macintosh models (yet)
  18.         public enum HardwareVersion
  19.         {
  20.             iPhone,
  21.             iPhone3G,
  22.             iPhone3GS,
  23.             iPhone4,
  24.             iPhone4S,
  25.             iPhone5,
  26.  
  27.             iPod1G,
  28.             iPod2G,
  29.             iPod3G,
  30.             iPod4G,
  31.             iPod5G,
  32.  
  33.             iPad,
  34.             iPad2,
  35.             iPad3,
  36.  
  37.             iPhoneSimulator,
  38.             iPhone4Simulator,
  39.             iPadSimulator,
  40.             Unknown
  41.         }
  42.  
  43.         // Changing the constant to "/usr/lib/libSystem.dylib" makes the P/Invoke work for Mac OS X also (tested), but returns only running arch (that's the thing it's getting in the simulator)
  44.         // For getting the Macintosh computer model property must be "hw.model" instead (and works on ppc, ppc64, i386 and x86_64 Mac OS X)
  45.         [DllImport(MonoTouch.Constants.SystemLibrary)]
  46.         static internal extern int sysctlbyname([MarshalAs(UnmanagedType.LPStr)] string property, IntPtr output, IntPtr oldLen, IntPtr newp, uint newlen);
  47.  
  48.         public static HardwareVersion Version
  49.         {
  50.             get
  51.             {
  52.                 Console.WriteLine("system library is {0}.", MonoTouch.Constants.SystemLibrary);
  53.                 var pLen = Marshal.AllocHGlobal(sizeof(int));
  54.                 sysctlbyname(HardwareProperty, IntPtr.Zero, pLen, IntPtr.Zero, 0);
  55.  
  56.                 var length = Marshal.ReadInt32(pLen);
  57.  
  58.                 if (length == 0)
  59.                 {
  60.                     Marshal.FreeHGlobal(pLen);
  61.  
  62.                     return HardwareVersion.Unknown;
  63.                 }
  64.  
  65.                 var pStr = Marshal.AllocHGlobal(length);
  66.                 sysctlbyname(HardwareProperty, pStr, pLen, IntPtr.Zero, 0);
  67.  
  68.                 var hardwareStr = Marshal.PtrToStringAnsi(pStr);
  69.                 var ret = HardwareVersion.Unknown;
  70.  
  71.                 if (hardwareStr == "iPhone1,1")
  72.                     ret = HardwareVersion.iPhone;
  73.                 else if (hardwareStr == "iPhone1,2")
  74.                     ret = HardwareVersion.iPhone3G;
  75.                 else if (hardwareStr == "iPhone2,1")
  76.                     ret = HardwareVersion.iPhone3GS;
  77.                 else if (hardwareStr == "iPhone3,1")
  78.                     ret = HardwareVersion.iPhone4;
  79.                 else if (hardwareStr == "iPhone4,1")
  80.                     ret = HardwareVersion.iPhone4S;
  81.                 else if (hardwareStr == "iPhone5,1")
  82.                     ret = HardwareVersion.iPhone5;
  83.                 else if (hardwareStr == "iPhone5,2")
  84.                     ret = HardwareVersion.iPhone5;
  85.  
  86.                 else if (hardwareStr == "iPad1,1")
  87.                     ret = HardwareVersion.iPad;
  88.                 else if (hardwareStr == "iPad2,1")
  89.                     ret = HardwareVersion.iPad2;
  90.                 else if (hardwareStr == "iPad3,1")
  91.                     ret = HardwareVersion.iPad3;
  92.  
  93.                 else if (hardwareStr == "iPod1,1")
  94.                     ret = HardwareVersion.iPod1G;
  95.                 else if (hardwareStr == "iPod2,1")
  96.                     ret = HardwareVersion.iPod2G;
  97.                 else if (hardwareStr == "iPod3,1")
  98.                     ret = HardwareVersion.iPod3G;
  99.                 else if (hardwareStr == "iPod4,1")
  100.                     ret = HardwareVersion.iPod4G;
  101.                 else if (hardwareStr == "iPod5,1")
  102.                     ret = HardwareVersion.iPod5G;
  103.  
  104.                 else if (hardwareStr == "i386" || hardwareStr == "x86_64")
  105.                 {
  106.                     if (UIDevice.CurrentDevice.Model.Contains("iPhone"))
  107.                         ret = UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale == 960 || UIScreen.MainScreen.Bounds.Width * UIScreen.MainScreen.Scale == 960 ? HardwareVersion.iPhone4Simulator : HardwareVersion.iPhoneSimulator;
  108.                     else
  109.                         ret = HardwareVersion.iPadSimulator;
  110.                 }
  111.  
  112.                 Marshal.FreeHGlobal(pLen);
  113.                 Marshal.FreeHGlobal(pStr);
  114.  
  115.                 return ret;
  116.             }
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment