Programmatically detect if app is being run on device or simulator

前端 未结 8 1840
我寻月下人不归
我寻月下人不归 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:47

    I created a macro in which you can specify which actions you want to perform inside parentheses and these actions will only be performed if the device is being simulated.

    #define SIM(x) if ([[[UIDevice currentDevice].model lowercaseString] rangeOfString:@"simulator"].location != NSNotFound){x;}
    

    This is used like this:

    SIM(NSLog(@"This will only be logged if the device is simulated"));
    
    0 讨论(0)
  • 2020-11-28 06:53

    TARGET_IPHONE_SIMULATOR is defined on the device (but defined to false). and defined as below

    #if TARGET_IPHONE_SIMULATOR
    NSString * const DeviceMode = @"Simulator";
    #else
    NSString * const DeviceMode = @"Device";
    #endif
    

    Just use DeviceMode to know between device and simulator

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