在Objective-C中,我们可以使用宏知道应用程序是为设备还是模拟器构建的:
#if TARGET_IPHONE_SIMULATOR
// Simulator
#else
// Device
#endif
这些是编译时宏,在运行时不可用。
我如何在Swift中实现同样的目标?
在Objective-C中,我们可以使用宏知道应用程序是为设备还是模拟器构建的:
#if TARGET_IPHONE_SIMULATOR
// Simulator
#else
// Device
#endif
这些是编译时宏,在运行时不可用。
我如何在Swift中实现同样的目标?
当前回答
下面是一个基于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的字典条目,它应该包含模拟器。
其他回答
更新30/01/19
虽然这个答案可能有效,但静态检查的推荐解决方案(正如几个苹果工程师所澄清的那样)是定义一个针对iOS模拟器的自定义编译器标志。有关如何操作的详细说明,请参阅@mbelsky的回答。
原来的答案
如果你需要静态检查(例如,不是运行时If /else),你不能直接检测模拟器,但你可以在桌面架构上检测iOS,如下所示
#if (arch(i386) || arch(x86_64)) && os(iOS)
...
#endif
Swift 4.1版本之后
最新的使用,现在直接适用于所有在一个条件下所有类型的模拟器只需要适用一个条件-
#if targetEnvironment(simulator)
// your simulator code
#else
// your real device code
#endif
要了解更多信息,请查看Swift提案SE-0190
旧版本-
显然,这在设备上是false,但对于iOS模拟器,它返回true,如文档中所指定的:
当为32位iOS模拟器编译代码时,arch(i386)构建配置返回true。
如果你正在为iOS以外的模拟器开发,你可以简单地改变操作系统参数:
检测watchOS模拟器
#if (arch(i386) || arch(x86_64)) && os(watchOS)
...
#endif
检测tvOS模拟器
#if (arch(i386) || arch(x86_64)) && os(tvOS)
...
#endif
或者,甚至检测任何模拟器
#if (arch(i386) || arch(x86_64)) && (os(iOS) || os(watchOS) || os(tvOS))
...
#endif
如果您可以使用运行时检查,则可以检查TARGET_OS_SIMULATOR变量(或iOS 8及以下版本的TARGET_IPHONE_SIMULATOR),这在模拟器上是正确的。
请注意,这与使用预处理器标志有所不同,并且有一些限制。例如,你不能在if/else语法无效的地方使用它(例如,在函数作用域之外)。
例如,假设您希望在设备和模拟器上有不同的导入。这在动态检查中是不可能的,而在静态检查中是微不足道的。
#if (arch(i386) || arch(x86_64)) && os(iOS)
import Foo
#else
import Bar
#endif
此外,由于该标志被swift预处理器替换为0或1,如果你直接在if/else表达式中使用它,编译器将发出关于不可访问代码的警告。
为了解决这个警告,请参阅其他答案之一。
斯威夫特4:
目前,我更喜欢使用ProcessInfo类来知道设备是否是模拟器以及正在使用的设备类型:
if let simModelCode = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] {
print("yes is a simulator :\(simModelCode)")
}
但是,正如您所知道的,simModelCode并不是一个容易理解的代码,因此,如果您需要,您可以尝试查看另一个so答案,以确定当前的iPhone/设备模型,并获得一个更适合人类阅读的字符串。
我不知道这是否对任何人有用,但至少M1 mac的当前版本似乎没有将SIMULATOR_MODEL_IDENTIFIER传递到NSProcessInfo
我使用
BOOL isMobile = [[NSProcessInfo processInfo].environment[@"USER"] isEqual:@"mobile"];
和迅捷的对等物。这可能很脆弱,但很有效。
来自Xcode 9.3
#if targetEnvironment(simulator)
Swift supports a new platform condition targetEnvironment with a single valid argument simulator. Conditional compilation of the form '#if targetEnvironment(simulator)' can now be used to detect when the build target is a simulator. The Swift compiler will attempt to detect, warn, and suggest the use of targetEnvironment(simulator) when evaluating platform conditions that appear to be testing for simulator environments indirectly, via the existing os() and arch() platform conditions. (SE-0190)
iOS 9+:
extension UIDevice {
static var isSimulator: Bool {
return NSProcessInfo.processInfo().environment["SIMULATOR_DEVICE_NAME"] != nil
}
}
斯威夫特3:
extension UIDevice {
static var isSimulator: Bool {
return ProcessInfo.processInfo.environment["SIMULATOR_DEVICE_NAME"] != nil
}
}
iOS 9之前:
extension UIDevice {
static var isSimulator: Bool {
return UIDevice.currentDevice().model == "iPhone Simulator"
}
}
objective - c:
@interface UIDevice (Additions)
- (BOOL)isSimulator;
@end
@implementation UIDevice (Additions)
- (BOOL)isSimulator {
if([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){9, 0, 0}]) {
return [NSProcessInfo processInfo].environment[@"SIMULATOR_DEVICE_NAME"] != nil;
} else {
return [[self model] isEqualToString:@"iPhone Simulator"];
}
}
@end
让我在这里澄清一些事情:
TARGET_OS_SIMULATOR在很多情况下在Swift代码中没有设置;由于桥接头,您可能会意外地导入它,但这是脆弱的,不受支持。这在框架中甚至是不可能的。这就是为什么有些人对这在Swift中是否有效感到困惑。 我强烈建议不要使用架构来代替模拟器。
执行动态检查:
检查ProcessInfo.processInfo。environment["SIMULATOR_DEVICE_NAME"] != nil完全没问题。
您还可以通过检查SIMULATOR_MODEL_IDENTIFIER来获得正在模拟的底层模型,它将返回像iPhone10,3这样的字符串。
执行静态检查:
Xcode 9.2及更早版本:定义你自己的Swift编译标志(如其他答案所示)。
Xcode 9.3+使用新的targetenvirenvironment条件:
#if targetEnvironment(simulator)
// for sim only
#else
// for device
#endif