在Objective-C中,我们可以使用宏知道应用程序是为设备还是模拟器构建的:

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

这些是编译时宏,在运行时不可用。

我如何在Swift中实现同样的目标?


当前回答

在现代系统中:

#if targetEnvironment(simulator)
    // sim
#else
    // device
#endif

就是这么简单。

其他回答

斯威夫特5.2.4 Xcode 11.7


 #if targetEnvironment(simulator)

 #endif

运行时,但比这里的大多数其他解决方案更简单:

if TARGET_OS_SIMULATOR != 0 {
    // target is current running in the simulator
}

或者,你可以调用一个Objective-C helper函数来返回一个使用预处理器宏的布尔值(特别是如果你已经在你的项目中混合了)。

编辑:不是最好的解决方案,特别是在Xcode 9.3中。请看HotJard的回答

更新信息截至2018年2月20日

看起来@russbishop有一个权威的答案,使这个答案“不正确”-即使它似乎工作了很长一段时间。

检测应用程序是否正在构建的设备或模拟器在Swift

以前的回答

基于@WZW的回答和@Pang的评论,我创建了一个简单的实用结构。这个解决方案避免了@WZW的答案产生的警告。

import Foundation

struct Platform {

    static var isSimulator: Bool {
        return TARGET_OS_SIMULATOR != 0
    }

}

使用示例:

if Platform.isSimulator {
    print("Running on Simulator")
}

下面是一个基于HotJard上面的精彩答案的Xcode 11 Swift示例,这也添加了一个isDevice Bool,并使用SIMULATOR_UDID而不是name。变量赋值是在每一行上完成的,这样你就可以更容易地在调试器中检查它们。

import Foundation

// Extensions to UIDevice based on ProcessInfo.processInfo.environment keys
// to determine if the app is running on an actual device or the Simulator.

@objc extension UIDevice {
    static var isSimulator: Bool {
        let environment = ProcessInfo.processInfo.environment
        let isSimulator = environment["SIMULATOR_UDID"] != nil
        return isSimulator
    }

    static var isDevice: Bool {
        let environment = ProcessInfo.processInfo.environment
        let isDevice = environment["SIMULATOR_UDID"] == nil
        return isDevice
    }
}

还有DTPlatformName的字典条目,它应该包含模拟器。

我在Swift 3中使用了下面的代码

if TARGET_IPHONE_SIMULATOR == 1 {
    //simulator
} else {
    //device
}