-
Notifications
You must be signed in to change notification settings - Fork 35
/
Xamarin.iOS.DeviceHardware.cs
96 lines (79 loc) · 3.1 KB
/
Xamarin.iOS.DeviceHardware.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Runtime.InteropServices;
using Foundation;
using ObjCRuntime;
namespace Xamarin.iOS
{
[Preserve(AllMembers = true)]
public static class DeviceHardware
{
private const string HardwareProperty = "hw.machine";
[DllImport(Constants.SystemLibrary)]
private static extern int sysctlbyname([MarshalAs(UnmanagedType.LPStr)] string property,
IntPtr output,
IntPtr oldLen,
IntPtr newp,
uint newlen);
private static readonly iOSHardware _hardwareMapper = new iOSHardware();
private static readonly Lazy<string> _version = new Lazy<string>(FindVersion);
private static string FindVersion()
{
try
{
// 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 "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);
// cleanup
Marshal.FreeHGlobal(pLen);
Marshal.FreeHGlobal(pStr);
return hardwareStr;
}
catch (Exception ex)
{
Console.WriteLine("DeviceHardware.Version Ex: " + ex.Message);
}
return "Unknown";
}
public static string Version => IsiOSAppOnMac() ? "mac" : FindVersion();
public static iOSChipType ChipType
{
get
{
var v = Version;
if (IsSimulator(v))
{
return _hardwareMapper.GetChipType(SimulatorModel);
}
return _hardwareMapper.GetChipType(v);
}
}
public static string Model
{
get
{
var v = Version;
if (IsSimulator(v))
{
return _hardwareMapper.GetModel(SimulatorModel) + " Simulator";
}
return _hardwareMapper.GetModel(v);
}
}
private static bool IsiOSAppOnMac() =>
UIKit.UIDevice.CurrentDevice.CheckSystemVersion(14, 0) && NSProcessInfo.ProcessInfo.IsiOSApplicationOnMac;
private static bool IsSimulator(string v) => v == "i386" || v == "x86_64";
private static string SimulatorModel => NSProcessInfo.ProcessInfo.Environment["SIMULATOR_MODEL_IDENTIFIER"].ToString();
}
}