【问题标题】:How to detect app is running on simulator or device [duplicate]如何检测应用程序正在模拟器或设备上运行[重复]
【发布时间】:2012-03-31 20:39:57
【问题描述】:

可能重复:
Programmatically detect if app is being run on device or simulator

如何通过代码检测我的应用是在模拟器上运行还是在设备上运行。

【问题讨论】:

标签: iphone objective-c ios ipad


【解决方案1】:

请记住,UIDevice 已经为您提供了有关设备本身的信息。

[[UIDevice currentDevice] model]

您还可以使用以下内容:

TARGET_IPHONE_SIMULATOR 告诉你是否在 iPhone 模拟器中。

TARGET_OS_IPHONE 告诉您您正在使用 iPhone 而不是 MacOS。

#if TARGET_IPHONE_SIMULATOR

    NSLog(@"Running in Simulator - no app store or giro");

#else

    NSLog(@"Running on the Device");

#endif

只对设备感兴趣时

#if !(TARGET_IPHONE_SIMULATOR)

    NSLog(@"Running on device");

#endif

【讨论】:

    【解决方案2】:

    你可以使用这个常量

    #if TARGET_OS_SIMULATOR
        NSLog(@"This is simulator mode....");
    #else
        NSLog(@"This is device mode....");
    #endif
    

    【讨论】:

      【解决方案3】:

      相同的编译应用不能在模拟器和 iOS 设备上运行,因为 CPU 指令集完全不同(x86 与 ARM)。 (...除非您正在使用 lipo 构建某种非常奇怪的超级通用二进制文件)

      有几种方法可以确定应用程序是否针对 x86 编译。一种是添加不同的运行时代码,具体取决于许多预定义的编译器预处理器宏之一。您可以通过在终端命令行上键入以下内容来获取 x86 编译的预处理器宏列表:

      gcc -arch i386 -dM -E -

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-12
        • 2016-02-19
        • 1970-01-01
        • 2012-09-18
        • 1970-01-01
        • 2014-07-11
        • 1970-01-01
        相关资源
        最近更新 更多