Programmatically detect if app is being run on device or simulator

前端 未结 8 1839
我寻月下人不归
我寻月下人不归 2020-11-28 06:14

I\'d like to know whether my app is being run on device or simulator at run time. Is there a way to detect this?

Reason being to test bluetooth api with simulator:

相关标签:
8条回答
  • 2020-11-28 06:29

    Check if simulator

    #if TARGET_IPHONE_SIMULATOR
    // Simulator
    #endif
    

    Check if device

    #if !(TARGET_IPHONE_SIMULATOR)
    // Device
    #endif
    

    Check for both

    #if TARGET_IPHONE_SIMULATOR
    // Simulator
    #else
    // Device
    #endif
    

    Please note that you should not ifdef on TARGET_IPHONE_SIMULATOR because it will always be defined to either 1 or 0.

    0 讨论(0)
  • 2020-11-28 06:31

    Use this below code:

    #if targetEnvironment(simulator)
       // iOS Simulator
    #else
       // Device
    #endif
    

    Works for Swift 4 and Xcode 9.4.1

    0 讨论(0)
  • 2020-11-28 06:38

    From XCode 9.3+ , Swift

    #if targetEnvironment(simulator)
    //Simulator
    #else
    //Real device
    #endif
    

    Helps you to code against device type specific.

    0 讨论(0)
  • 2020-11-28 06:39
    #if TARGET_OS_SIMULATOR
    
    //Simulator
    
    #else
    
    // Device
    
    #endif
    

    Pls refer this previous SO question also What #defines are set up by Xcode when compiling for iPhone

    0 讨论(0)
  • 2020-11-28 06:40

    if anyone is looking for Unity solution i did this, the only way i found how.

    using System.Globalization;
    
    public static bool IsArm() {
            return CultureInfo.InvariantCulture.CompareInfo.IndexOf(SystemInfo.processorType, "ARM", CompareOptions.IgnoreCase) >= 0;
        }
    
    0 讨论(0)
  • 2020-11-28 06:46

    You can use the TARGET_IPHONE_SIMULATOR preprocessor macro to distinguish between device and simulator targets.

    0 讨论(0)
提交回复
热议问题