0

In my Xamarin iOS application, I can obtain many device characteristics such as model, system name, etc. from UIKit.UIDevice.CurrentDevice instance. However, I don't see any method to obtain CPU architecture (x86, arm, etc.) on the class.

How can I get the iOS device CPU architecture in runtime shows a way to get this information using Objective C. I am wondering if there a way to get the CPU information in C# using any of the predefined Xamarin classes.

Peter
  • 11,260
  • 14
  • 78
  • 155

2 Answers2

2

Create a new file with this class on your iOS project:

public static class DeviceInfo
{
    public const string HardwareSysCtlName = "hw.machine";

    public static string HardwareArch { get; private set; }

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

    static DeviceInfo()
    {
        var pLen = Marshal.AllocHGlobal(sizeof(int));
        sysctlbyname(HardwareSysCtlName, IntPtr.Zero, pLen, IntPtr.Zero, 0);

        var length = Marshal.ReadInt32(pLen);

        var pStr = Marshal.AllocHGlobal(length);
        sysctlbyname(HardwareSysCtlName, pStr, pLen, IntPtr.Zero, 0);

        HardwareArch = Marshal.PtrToStringAnsi(pStr);
    }
}
J. Aguilar
  • 194
  • 5
  • Thank you very much for your help. Although "hw.machine" is not what I needed, your code did give me an idea on what I need to do. – Peter Aug 28 '18 at 16:32
0

Today, I found that this code works not only for Windows(UWP), but also for iOS With Xamarin:

System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture.ToString();

With iOS Simulator, RuntimeInformation has more information, like OSArchitecture, but with real iOS device, only ProcessArchitecture was available.

I used all available recent softwares: Xcode 12.4, Visual Studio for Mac 8.9 (1651), etc.

Brian Hong
  • 930
  • 11
  • 15