【问题标题】:Determine if iOS device is 32- or 64-bit确定 iOS 设备是 32 位还是 64 位
【发布时间】:2013-11-20 18:49:22
【问题描述】:

有谁知道判断 iOS7 设备是 32 位还是 64 位硬件的简单方法?我不是指以编程方式,我只是指通过设置、型号、第 3 方应用等。

我遇到了一个我怀疑与 64 位相关的问题。 Apple 的建议是在 64 位模拟器上进行测试,但也在实际的 64 位设备上进行测试,但没有说明如何确定这一点。我可以编写一个测试应用程序来检查 sizeof(int) 或其他什么,但必须有某种方法让技术支持人员知道他们正在使用什么。

埃里克

【问题讨论】:

  • 看型号就知道了。 iPhone 5s、iPad mini Retina 和 iPad Air 是 64 位的。其他的是 32 位的。
  • 使用模型也是我得出的结论。我想从现在起两年后的型号就足够了,我试图弄清楚客户正在运行什么(“让我们看看,是 5C 32 位......?”)
  • 在运行时,您可以通过下一个解决方案stackoverflow.com/questions/20104403/… 检测设备拱门

标签: ios


【解决方案1】:

没有其他“官方”方法可以确定它。您可以使用以下代码确定它:

if (sizeof(void*) == 4) {
    NSLog(@"32-bit App");
} else if (sizeof(void*) == 8) {
    NSLog(@"64-bit App");
}

【讨论】:

  • 这会检查 应用程序 是在 32 位还是 64 位模式下运行,而不是在 hardware 是 32 位还是 64 位模式下运行。
  • 根据 Apple 的说法,当您编译 64 位时,32 位和 64 位二进制文​​件都包含在 .ipa 文件中。因此,如果应用程序在 64 位模式下运行,那么硬件就是 64 位。
  • @NikosM.,你有消息来源吗?
  • @NikosM.:是的,但是 32 位应用程序也可以在 64 位硬件上运行。 (顺便说一句,没有投反对票。)
  • 如果应用程序被编译为 32/64 位“通用”二进制,那么这两个答案都可以使用,因为 64 位切片将在 64 位硬件上运行,而 32 位切片将在将在 32 位硬件上运行。我只是想指出sizeof(void *)#ifdef __LP64__ 是由编译器决定的,而不是由硬件决定的。
【解决方案2】:

下面是64位硬件的方法。如果硬件是 64 位硬件并且可以在真实的 iOS 设备和 iOS 模拟器中运行,则返回 YES。这里是source

#include <mach/mach.h>

+ (BOOL) is64bitHardware
{
#if __LP64__
    // The app has been compiled for 64-bit intel and runs as 64-bit intel
    return YES;
#endif

    // Use some static variables to avoid performing the tasks several times.
    static BOOL sHardwareChecked = NO;
    static BOOL sIs64bitHardware = NO;

    if(!sHardwareChecked)
    {
        sHardwareChecked = YES;

#if TARGET_IPHONE_SIMULATOR
        // The app was compiled as 32-bit for the iOS Simulator.
        // We check if the Simulator is a 32-bit or 64-bit simulator using the function is64bitSimulator()
        // See http://blog.timac.org/?p=886
        sIs64bitHardware = is64bitSimulator();
#else
        // The app runs on a real iOS device: ask the kernel for the host info.
        struct host_basic_info host_basic_info;
        unsigned int count;
        kern_return_t returnValue = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)(&host_basic_info), &count);
        if(returnValue != KERN_SUCCESS)
        {
            sIs64bitHardware = NO;
        }

        sIs64bitHardware = (host_basic_info.cpu_type == CPU_TYPE_ARM64);

#endif // TARGET_IPHONE_SIMULATOR
    }

    return sIs64bitHardware;
}

【讨论】:

  • 如果您复制完整的功能,请链接到您的source
【解决方案3】:

完全未经测试,但您应该可以通过sysctl 获取 CPU,如下所示:

#include <sys/types.h>
#include <sys/sysctl.h>
#include <mach/machine.h>

void foo() {
    size_t size;
    cpu_type_t type;

    size = sizeof(type);
    sysctlbyname("hw.cputype", &type, &size, NULL, 0);

    if (type == CPU_TYPE_ARM64) {
        // ARM 64-bit CPU
    } else if (type == CPU_TYPE_ARM) {
        // ARM 32-bit CPU
    } else {
        // Something else.
    }
}

在 iOS 7 SDK 中,CPU_TYPE_ARM64&lt;mach/machine.h&gt; 中定义为:

#define CPU_TYPE_ARM64          (CPU_TYPE_ARM | CPU_ARCH_ABI64)

另一种方式似乎是:

#include <mach/mach_host.h>

void foo() {
    host_basic_info_data_t hostInfo;
    mach_msg_type_number_t infoCount;

    infoCount = HOST_BASIC_INFO_COUNT;
    host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, &infoCount);

    if (hostInfo.cpu_type == CPU_TYPE_ARM64) {
        // ARM 64-bit CPU
    } else if (hostInfo.cpu_type == CPU_TYPE_ARM) {
        // ARM 32-bit CPU
    } else {
        // Something else.
    }
}

【讨论】:

    【解决方案4】:

    如果你使用 clang 编译,还有另一种方法:检查是否定义了 __arm____arm64__

    下面的示例代码未经测试,但它应该说明我的意思:

    #if defined(__arm__)
        NSLog(@"32-bit App");
    #elif defined(__arm64__)
        NSLog(@"64-bit App");
    #else
        NSLog(@"Not running ARM");
    #endif
    

    请注意,这取决于当前 iOS 应用程序二进制文件在单个容器中同时包含 32 位和 64 位二进制文​​件这一事实,并且它们将根据您的应用程序是否支持执行 64 位来正确选择。

    【讨论】:

      【解决方案5】:

      您可以在Int 上使用bitWidth https://developer.apple.com/documentation/swift/int/2885648-bitwidth

      static var is32Bit: Bool {
          return Int.bitWidth == 32
      }
      
      static var is64Bit: Bool {
          return Int.bitWidth == 64
      }
      

      【讨论】:

        【解决方案6】:

        我在 swift 4 中使用它,不确定它是否是最好的解决方案,但它确实有效。

           func getCPUArch()
           {
              #if arch(arm)
                 print("this is a 32bit system")
              #elseif arch(arm64)
                  print("this is a 64bit system")
              #endif
           }
        

        【讨论】:

          【解决方案7】:

          在运行时你可以使用这样的东西

          extension UIDevice {
              static let is64Bit = MemoryLayout<Int>.size == MemoryLayout<Int64>.size
          }
          

          【讨论】:

            猜你喜欢
            • 2011-04-04
            • 1970-01-01
            • 2011-04-19
            • 2010-10-23
            • 1970-01-01
            • 2010-09-19
            • 2012-01-25
            • 1970-01-01
            • 2012-03-17
            相关资源
            最近更新 更多