正如问题所述,我主要想知道我的代码是否在模拟器中运行,但也有兴趣知道正在运行或正在模拟的特定iphone版本。
编辑:我在问题名称中添加了“以编程方式”这个词。我的问题的要点是能够动态包括/排除代码取决于哪个版本/模拟器正在运行,所以我真的在寻找像一个预处理程序指令,可以为我提供这个信息。
正如问题所述,我主要想知道我的代码是否在模拟器中运行,但也有兴趣知道正在运行或正在模拟的特定iphone版本。
编辑:我在问题名称中添加了“以编程方式”这个词。我的问题的要点是能够动态包括/排除代码取决于哪个版本/模拟器正在运行,所以我真的在寻找像一个预处理程序指令,可以为我提供这个信息。
当前回答
在Swift的情况下,我们可以实现以下
我们可以创建struct,它允许你创建结构化数据
struct Platform {
static var isSimulator: Bool {
#if targetEnvironment(simulator)
// We're on the simulator
return true
#else
// We're on a device
return false
#endif
}
}
然后,如果我们想检测应用程序是否正在构建的设备或模拟器在Swift然后。
if Platform.isSimulator {
// Do one thing
} else {
// Do the other
}
其他回答
用swift:
#if (arch(i386) || arch(x86_64))
...
#endif
从检测应用程序是否正在构建的设备或模拟器在Swift
适用于Swift 4.1及更新版本和Xcode 9.3及更新版本
使用以下代码:
#if targetEnvironment(simulator)
// Simulator
#else
// Device
#endif
///如果它的模拟器不是设备,则返回true
public static var isSimulator: Bool {
#if (arch(i386) || arch(x86_64)) && os(iOS)
return true
#else
return false
#endif
}
如果没有效果,试试这个
public struct Platform {
public static var isSimulator: Bool {
return TARGET_OS_SIMULATOR != 0 // Use this line in Xcode 7 or newer
}
}
不是预处理器指令,但这是我在研究这个问题时所寻找的;
NSString *model = [[UIDevice currentDevice] model];
if ([model isEqualToString:@"iPhone Simulator"]) {
//device is simulator
}