ChatGPT解决这个技术问题 Extra ChatGPT

How to detect if app is being built for device or simulator in Swift

In Objective-C we can know if an app is being built for device or simulator using macros:

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

These are compile time macros and not available at runtime.

How can I achieve the same in Swift?

That is not how to detect the simulator or a real device at runtime in Objective-C. Those are compiler directives that result in different code depending on the build.
Thanks. I edited my question.
THE HIGHEST VOTED ANSWERS ARE NOT THE BEST WAY TO SOLVE THIS PROBLEM! mbelsky's answer (currently very far down) is the only solution that comes without any pitfalls. Even Greg Parker from Apple suggested to do it that way: lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160125/…
EVEN IN CAPS, IT IS NAIVE TO SUGGEST THAT THERE IS ANYTHING WRONG WITH A RUNTIME CHECK. Suggestions by Apple engineers are often poorly-thought out garbage, or only apply in certain situations, so that initself means less than nothing.
@Fattie: It would be interesting to know why none of the given answers satisfies your needs, and what you are exactly hoping for by offering the bounty.

A
Anand Nimje

Update 30/01/19

While this answer may work, the recommended solution for a static check (as clarified by several Apple engineers) is to define a custom compiler flag targeting iOS Simulators. For detailed instructions on how to do to it, see @mbelsky's answer.

Original answer

If you need a static check (e.g. not a runtime if/else) you can't detect the simulator directly, but you can detect iOS on a desktop architecture like follows

#if (arch(i386) || arch(x86_64)) && os(iOS)
    ...
#endif

After Swift 4.1 version

Latest use, now directly for all in one condition for all types of simulators need to apply only one condition -

#if targetEnvironment(simulator)
  // your simulator code
#else
  // your real device code
#endif

For more clarification, you can check Swift proposal SE-0190

For older version -

Clearly, this is false on a device, but it returns true for the iOS Simulator, as specified in the documentation:

The arch(i386) build configuration returns true when the code is compiled for the 32–bit iOS simulator.

If you are developing for a simulator other than iOS, you can simply vary the os parameter: e.g.

Detect the watchOS simulator

#if (arch(i386) || arch(x86_64)) && os(watchOS)
...
#endif

Detect the tvOS simulator

#if (arch(i386) || arch(x86_64)) && os(tvOS)
...
#endif

Or, even, detect any simulator

#if (arch(i386) || arch(x86_64)) && (os(iOS) || os(watchOS) || os(tvOS))
...
#endif

If you instead are ok with a runtime check, you can inspect the TARGET_OS_SIMULATOR variable (or TARGET_IPHONE_SIMULATOR in iOS 8 and below), which is truthy on a simulator.

Please notice that this is different and slightly more limited than using a preprocessor flag. For instance you won't be able to use it in place where a if/else is syntactically invalid (e.g. outside of functions scopes).

Say, for example, that you want to have different imports on the device and on the simulator. This is impossible with a dynamic check, whereas it's trivial with a static check.

#if (arch(i386) || arch(x86_64)) && os(iOS)
  import Foo
#else
  import Bar
#endif

Also, since the flag is replaced with a 0 or a 1 by the swift preprocessor, if you directly use it in a if/else expression the compiler will raise a warning about unreachable code.

In order to work around this warning, see one of the other answers.


More reading here. And to be even more restrictive, you could use arch(i386) && os(iOS).
This did not work for me. I had to check for both i386 and x86_64
THIS ANSWER IS NOT THE BEST WAY TO SOLVE THIS PROBLEM! mbelsky's answer (currently very far down) is the only solution that comes without any pitfalls. Even Greg Parker from Apple suggested to do it that way: lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160125/…
@russbishop this proved to be helpful advice to hundreds of people so far, compensating for a missing API. Instead of hijacking the answer by signing a comment on top, just communicate. I updated the answer to clarify this is no longer an up-to-date solution and I've provided a link to the one that looks more correct.
In Swift 4.1, you'll be able to say #if targetEnvironment(simulator) :) (github.com/apple/swift-evolution/blob/master/proposals/…)
m
mbelsky

OUTDATED FOR SWIFT 4.1. Use #if targetEnvironment(simulator) instead. Source

To detect simulator in Swift you can use build configuration:

Define this configuration -D IOS_SIMULATOR in Swift Compiler - Custom Flags > Other Swift Flags

Select Any iOS Simulator SDK in this drop down

Now you could use this statement to detect simulator:

#if IOS_SIMULATOR
    print("It's an iOS Simulator")
#else
    print("It's a device")
#endif

Also you could extend UIDevice class:

extension UIDevice {
    var isSimulator: Bool {
        #if IOS_SIMULATOR
            return true
        #else
            return false
        #endif
    }
}
// Example of usage: UIDevice.current.isSimulator

This should be the best answer! Even Greg Parker from Apple suggested that way: lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160125/…
usage update for swift 3: UIDevice.current.isSimulator
May i ask why if i add this under Release this doesn't work?
This is the only correct answer. You can also set this up in xcconfig files by using OTHER_SWIFT_FLAGS = TARGET_OS_EMBEDDED and OTHER_SWIFT_FLAGS[sdk=embeddedsimulator*] = TARGET_OS_SIMULATOR to override for the Simulator.
On Xcode 9.2, this answer was failing to compile some of the time. Removing the "-" before the "D" solved the issue for me.
D
Daniel

Updated Info as of February 20, 2018

It looks like @russbishop has an authoritative answer that renders this answer "incorrect" - even though it appeared to work for a long time.

Detect if app is being built for device or simulator in Swift

Previous Answer

Based on @WZW's answer and @Pang's comments, I created a simple utility struct. This solution avoids warning produced by @WZW's answer.

import Foundation

struct Platform {

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

}

Example usage:

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

Much better solution than accepted one. Indeed if some day (even though it's very unlikely) Apple decide to use i386 or x85_64 on iOS devices, the accepted answer won't work… or even if desktop computers get a new proc!
Confirmed that this works perfectly on Xcode 7: public let IS_SIMULATOR = (TARGET_OS_SIMULATOR != 0)... same thing, simplified. +1 thanks
@daniel This works well and it's actually more straightforward than my solution. However it's worth noting that it's more limited than an actual preprocessor step. If you need some part of the code not to be included in the target (e.g. you want to choose between two imports at compile time), you have to use a static check. I've edited my answer to highlight this difference.
THIS ANSWER IS NOT THE BEST WAY TO SOLVE THIS PROBLEM! mbelsky's answer (currently very far down) is the only solution that comes without any pitfalls. Even Greg Parker from Apple suggested to do it that way: lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160125/…
@Fattie TARGET_OS_SIMULATOR != 0 is already in the answer. It's the solution given by Daniel. There's no need to add it again in a free variable, it's already there. If you think having it in a struct is bad and having it in a free variable is better then post a comment about this or make your own answer. Thanks.
H
HotJard

From 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
    }
}

Swift 3:

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

Before 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

Comparing strings is more fragile that using defined constants.
@P1X3L5 you are right! But I'm assume that this method is called in debug mode - it couldn't be so solid, but quick to add to a project
@GantMan thanks for the response. I've fixed the code
@HotJard nice, this one does not produce will never be executed warning
A
Ahmad F

Swift 4

You can now use targetEnvironment(simulator) as an argument.

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

Updated for Xcode 9.3


This should now be the accepted answer. I wish there was a way on SO to propose a new suggested answer based on updates to OS / programming languages.
it's a great point @quemeful - it's one of the few basic failings of SO. Since computing systems change so quickly, almost every answer on SO becomes wrong over time.
r
russbishop

Let me clarify some things here:

TARGET_OS_SIMULATOR is not set in Swift code in many cases; you may be accidentally getting it imported due to a bridging header but this is brittle and not supported. It also isn't even possible in frameworks. This is why some people are confused about whether this works in Swift. I strongly advise against using architecture as a substitute for simulator.

To perform dynamic checks:

Checking ProcessInfo.processInfo.environment["SIMULATOR_DEVICE_NAME"] != nil is perfectly fine.

You can also get the underlying model being simulated by checking SIMULATOR_MODEL_IDENTIFIER which will return strings like iPhone10,3.

To perform static checks:

Xcode 9.2 & earlier: define your own Swift compilation flag (as shown in other answers).

Xcode 9.3+ use the new targetEnvironment condition:

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

It looks like you have some new inside info here. Very helpful! Note TARGET_OS_SIMULATOR worked for quite some time in both app and framework code; and it's also working in Xcode 9.3 b3. But, I guess this is "accidental". Kind of a bummer; because this seems like the least hacky way. As a provider of framework code that could be compiled in either Xcode 9.3 or earlier, it looks like we will have to wrap #if targetEnvironment... in a #if swift(>=4.1) macro to avoid compiler errors. Or I guess use ....environment["SIMULATOR_DEVICE_NAME"] != nil. This check seems more hacky, IMO.
if have " Unexpected platform condition (expected 'os', 'arch', or 'swift') " error using targetEnvironment(simulator)
@Aleksandr targetEnvironment landed in Xcode 9.3. You need a newer version of Xcode.
@russbishop good work clearing this up for the latest new era - thanks!
I sent a 250 bounty, since this answer seems to add the most and newest information - cheers
C
Community

What works for me since Swift 1.0 is checking for an architecture other than arm:

#if arch(i386) || arch(x86_64)

     //simulator
#else 
     //device

#endif

s
shim

Runtime, but simpler than most of the other solutions here:

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

Alternatively, you can just call an Objective-C helper function that returns a boolean that uses the preprocessor macro (especially if you're already mixing in your project).

Edit: Not the best solution, especially as of Xcode 9.3. See HotJard's answer


I do this but get warnings in the else clause because it "will never be executed". We have a zero warning rule, so :-(
it will show a warning but it makes sense, depending if you have simulator or device selected for building, the warning will show on the part that wont be executed, but yeah annoying for a zero warning policy
Only seeing warnings when I use == 0 instead of != 0. Using it as written above, even with an else block after, does not produce any warnings in Swift 4 Xcode Version 9.2 (9C40b)
Also I tested it running on a simulator target as well as a physical device. Also seems to be the same in Swift 3.2 (same Xcode version).
In Xcode 9.3 + Swift 4.1 I just noticed that it does have the warning even with !=0. Sheesh.
h
hectorsvill

Swift 5.2.4 Xcode 11.7


 #if targetEnvironment(simulator)

 #endif


F
Fattie

In modern systems:

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

It's dat easy.


Not sure why the first one should be "more correct" than Daniel's answer. – Note that the second one is a compile time check. Happy New Year!
A
Arsen Khachaturyan

I hope this extension comes handy.

extension UIDevice {
    static var isSimulator: Bool = {
        #if targetEnvironment(simulator)
        return true
        #else
        return false
        #endif
    }()
}

Usage:

if UIDevice.isSimulator {
    print("running on simulator")
}

@ChetanKoli, I was going for making the code very clear, rather than short, so its easy to understand for anyone. Not sure how i feel about your edit.
N
Nuthatch

TARGET_IPHONE_SIMULATOR is deprecated in iOS 9. TARGET_OS_SIMULATOR is the replacement. Also TARGET_OS_EMBEDDED is available.

From TargetConditionals.h :

#if defined(__GNUC__) && ( defined(__APPLE_CPP__) || defined(__APPLE_CC__) || defined(__MACOS_CLASSIC__) ) . . . #define TARGET_OS_SIMULATOR 0 #define TARGET_OS_EMBEDDED 1 #define TARGET_IPHONE_SIMULATOR TARGET_OS_SIMULATOR /* deprecated */ #define TARGET_OS_NANO TARGET_OS_WATCH /* deprecated */


i tried TARGET_OS_SIMULATOR but doesn't work or get recognized by the Xcode while TARGET_IPHONE_SIMULATOR does. I'm building for iOS 8.0 above.
I'm looking at the iOS 9 headers. I'll update my answer.
w
webcpu

Xcode 11, Swift 5

    #if !targetEnvironment(macCatalyst)
    #if targetEnvironment(simulator)
        true
    #else
        false        
    #endif
    #endif

x
xgerrit

In Xcode 7.2 (and earlier but I haven't tested how much earlier), you can set a platform specific build flag "-D TARGET_IPHONE_SIMULATOR" for "Any iOS Simulator".

Look in the project build settings under "Swift Compiler - Customer Flags" and then set the flag in "Other Swift Flags". You can set a platform specific flag by clicking the 'plus' icon when you hover over a build configuration.

There are a couple of advantages of doing it this way: 1) You can use the same conditional test ("#if TARGET_IPHONE_SIMULATOR") in your Swift and Objective-C code. 2) You can compile out variables that only apply to each build.

Xcode build settings screenshot


d
dimpiax

All described here Darwin.TargetConditionals: https://github.com/apple/swift-corelibs-foundation/blob/master/CoreFoundation/Base.subproj/SwiftRuntime/TargetConditionals.h

TARGET_OS_SIMULATOR - Generated code will run under a simulator


H
Haroldo Gondim

Use this below code:

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

Works for Swift 4 and Xcode 9.4.1


A
Alex Zavatone

Here is an Xcode 11 Swift example based off of HotJard's awesome answer above, this also adds an isDevice Bool and uses SIMULATOR_UDID instead of name. Variable assignments are done on each line so that you can more easily examine them in the debugger if you choose to.

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
    }
}

There also is the dictionary entry of DTPlatformName which should contain simulator.


M
M. Adam

In addition to other answers.

In Objective-c, Just make sure you included TargetConditionals.

#include <TargetConditionals.h>

before using TARGET_OS_SIMULATOR.


a
ak_ninan

I used this below code in Swift 3

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

I do this but get warnings in the else clause because it "will never be executed". We have a zero warning rule, so grrrr....
It will show a warning whenever you are trying to run with a device, if you are selected simulator for running it won't show the warning.
it's deprecated
A
Alessandro Ornano

Swift 4:

Currently, I prefer to use the ProcessInfo class to know if the device is a simulator and which kind of device is in use:

if let simModelCode = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] {
            print("yes is a simulator :\(simModelCode)")
}

But, as you know, simModelCode isn't a comfortable code to understand immediatly which kind of simulator was launched so, if your need, you can try to see this other SO answer to determine the current iPhone/device model and to have a more human readable string.