How can I programmatically determine if my app is running in the iphone simulator?
As the question states, I would mainly like to know whether or not my code is running in the simulator, but would also be interested in knowing the specific iphone version that is running or being simulated.
EDIT: I added the word 'programmatically' to the question name. The point of my question is to be able to dynamically include / exclude code depending on which version / simulator is running, so I'd really be looking for something like a pre-processor directive that can provide me this info.
ios objective-c swift xcode ios-simulator
add a comment |
As the question states, I would mainly like to know whether or not my code is running in the simulator, but would also be interested in knowing the specific iphone version that is running or being simulated.
EDIT: I added the word 'programmatically' to the question name. The point of my question is to be able to dynamically include / exclude code depending on which version / simulator is running, so I'd really be looking for something like a pre-processor directive that can provide me this info.
ios objective-c swift xcode ios-simulator
I'm not sure a pre-processor directive is dynamic (though it might be what you were looking for anyway). The directive means that you actually knew, when you built it, where it was going to wind up running.
– WiseOldDuck
Apr 15 '16 at 19:18
add a comment |
As the question states, I would mainly like to know whether or not my code is running in the simulator, but would also be interested in knowing the specific iphone version that is running or being simulated.
EDIT: I added the word 'programmatically' to the question name. The point of my question is to be able to dynamically include / exclude code depending on which version / simulator is running, so I'd really be looking for something like a pre-processor directive that can provide me this info.
ios objective-c swift xcode ios-simulator
As the question states, I would mainly like to know whether or not my code is running in the simulator, but would also be interested in knowing the specific iphone version that is running or being simulated.
EDIT: I added the word 'programmatically' to the question name. The point of my question is to be able to dynamically include / exclude code depending on which version / simulator is running, so I'd really be looking for something like a pre-processor directive that can provide me this info.
ios objective-c swift xcode ios-simulator
ios objective-c swift xcode ios-simulator
edited Aug 8 '18 at 13:10
Haroldo Gondim
3,08582650
3,08582650
asked Jan 19 '09 at 16:55
Jeffrey MeyerJeffrey Meyer
2,75852425
2,75852425
I'm not sure a pre-processor directive is dynamic (though it might be what you were looking for anyway). The directive means that you actually knew, when you built it, where it was going to wind up running.
– WiseOldDuck
Apr 15 '16 at 19:18
add a comment |
I'm not sure a pre-processor directive is dynamic (though it might be what you were looking for anyway). The directive means that you actually knew, when you built it, where it was going to wind up running.
– WiseOldDuck
Apr 15 '16 at 19:18
I'm not sure a pre-processor directive is dynamic (though it might be what you were looking for anyway). The directive means that you actually knew, when you built it, where it was going to wind up running.
– WiseOldDuck
Apr 15 '16 at 19:18
I'm not sure a pre-processor directive is dynamic (though it might be what you were looking for anyway). The directive means that you actually knew, when you built it, where it was going to wind up running.
– WiseOldDuck
Apr 15 '16 at 19:18
add a comment |
22 Answers
22
active
oldest
votes
Already asked, but with a very different title.
What #defines are set up by Xcode when compiling for iPhone
I'll repeat my answer from there:
It's in the SDK docs under "Compiling source code conditionally"
The relevant definition is TARGET_OS_SIMULATOR, which is defined in /usr/include/TargetConditionals.h within the iOS framework. On earlier versions of the toolchain, you had to write:
#include "TargetConditionals.h"
but this is no longer necessary on the current (Xcode 6/iOS8) toolchain.
So, for example, if you want to check that you are running on device, you should do
#if TARGET_OS_SIMULATOR
// Simulator-specific code
#else
// Device-specific code
#endif
depending on which is appropriate for your use-case.
1
Thanks. I agree with you this is a more specific version of your original question. If yours had come up in my original search, I wouldn't have even needed to ask.
– Jeffrey Meyer
Jan 19 '09 at 21:06
4
Be careful with these definitions. When you compile code with menu item 'Project > Set Active SDK > Simulator…', as TARGET_IPHONE_SIMULATOR as TARGET_OS_IPHONE variables are both defined! So the only right way to separate logic is pointed out below by Pete (Thanks dude).
– Vadim
Jan 24 '09 at 2:05
4
Watch the #if and #ifdef difference. For me it was the cause of incorrect behavior.
– Anton
Jan 9 '10 at 9:32
6
Perhaps the need to include TargetConditionals has been obviated since this was written, but just wanted to note that #if TARGET_IPHONE_SIMULATOR works without including TargetConditionals.h now.
– dmur
Mar 4 '14 at 23:34
2
how would this work in swift?
– Oren
Jul 31 '15 at 19:05
|
show 10 more comments
Updated code:
This is purported to work officially.
#if TARGET_IPHONE_SIMULATOR
NSString *hello = @"Hello, iPhone simulator!";
#elif TARGET_OS_IPHONE
NSString *hello = @"Hello, device!";
#else
NSString *hello = @"Hello, unknown target!";
#endif
Original post (since deprecated)
This code will tell you if you are running in a simulator.
#ifdef __i386__
NSLog(@"Running in the simulator");
#else
NSLog(@"Running on a device");
#endif
7
As of iOS 8 and Xcode 6.1.1 the TARGET_OS_IPHONE is true on the simulator.
– malhal
Jan 11 '15 at 20:28
2
this doesn't worik anymore on newer XCode versions
– Fabio Napodano
Jan 25 '16 at 18:01
1
Unless you are in 2016 and run a 64 bit simulator. Or in 2019 and run your code on an iPhone with Intel processor.
– gnasher729
Apr 5 '16 at 8:40
add a comment |
Not pre-processor directive, but this was what I was looking for when i came to this question;
NSString *model = [[UIDevice currentDevice] model];
if ([model isEqualToString:@"iPhone Simulator"]) {
//device is simulator
}
9
[model compare:iPhoneSimulator] == NSOrderedSame
should be written as[model isEqualToString:iPhoneSimulator]
– user102008
Jan 11 '11 at 22:51
17
Or[model hasSuffix:@"Simulator"]
if you only care about "simulator" in general, not iPhone or iPad in particular. This answer won't work for iPad simulator :)
– Nuthatch
Aug 12 '14 at 15:37
Upvoted because Nuthatch's comment makes this the best answer in toto.
– Le Mot Juiced
Apr 9 '15 at 19:52
9
No longer works on Simulator for iOS9!
– KlimczakM
Oct 16 '15 at 10:17
11
In iOS9, check the devicename
instead ofmodel
– n.Drake
Oct 29 '15 at 8:51
|
show 1 more comment
The best way to do this is:
#if TARGET_IPHONE_SIMULATOR
and not
#ifdef TARGET_IPHONE_SIMULATOR
since its always defined: 0 or 1
add a comment |
In case of Swift we can implement following
We can create struct which allows you to create a structured data
struct Platform {
static let isSimulator: Bool = {
#if arch(i386) || arch(x86_64)
return true
#endif
return false
}()
}
Then If we wanted to Detect if app is being built for device or simulator in Swift then .
if Platform.isSimulator {
// Do one thing
}
else {
// Do the other
}
Cleanest implementation in my opinion, and it accounts for x86_64 and i386 architectures. Helped me overcome a weird device vs. simulator bug in Core Data. You're the man!
– Iron John Bonney
May 26 '16 at 22:01
thanks for the complement dude
– Nischal Hada
May 27 '16 at 8:35
Really cool answer.
– DawnSong
Nov 9 '17 at 9:47
thanks buddy @Dawn Song
– Nischal Hada
Nov 10 '17 at 6:19
5
In Playground, you will get a warning, "Code after 'return' will never be executed". So I think#if #else #endif
will be better.
– DawnSong
Nov 10 '17 at 8:40
add a comment |
THERE IS A BETTER WAY NOW!
As of Xcode 9.3 beta 4 you can use #if targetEnvironment(simulator)
to check.
#if targetEnvironment(simulator)
//Your simulator code
#endif
UPDATE
Xcode 10 and iOS 12 SDK supports this too.
3
This should be the new accepted answer.
– Dan Loewenherz
Apr 4 '18 at 19:03
This is the only that works for me, rest of the solutions didn't work.
– Vrutin Rathod
May 17 '18 at 15:59
this proposal has been accepted in swift 4.1
– Hamsternik
Jun 15 '18 at 13:22
add a comment |
All those answer are good, but it somehow confuses newbie like me as it does not clarify compile check and runtime check. Preprocessor are before compile time, but we should make it clearer
This blog article shows How to detect the iPhone simulator? clearly
Runtime
First of all, let’s shortly discuss. UIDevice provides you already information about the device
[[UIDevice currentDevice] model]
will return you “iPhone Simulator” or “iPhone” according to where the app is running.
Compile time
However what you want is to use compile time defines. Why? Because you compile your app strictly to be run either inside the Simulator or on the device. Apple makes a define called TARGET_IPHONE_SIMULATOR
. So let’s look at the code :
#if TARGET_IPHONE_SIMULATOR
NSLog(@"Running in Simulator - no app store or giro");
#endif
1
How does this improve on other answers?
– Mark
Jun 17 '14 at 16:42
@Mark It clarifies a little bit
– onmyway133
Jun 18 '14 at 16:52
5
Currently, in Xcode 7, iOS 9 Simulator[[UIDevice currentDevice] model]
is returningiPhone
also instead ofiPhone Simulator
. So, I think this is not the best approach.
– eMdOS
Jan 12 '16 at 19:11
add a comment |
The previous answers are a little dated. I found that all you need to do is query the TARGET_IPHONE_SIMULATOR
macro (no need to include any other header files [assuming you are coding for iOS]).
I attempted TARGET_OS_IPHONE
but it returned the same value (1) when running on an actual device and simulator, that's why I recommend using TARGET_IPHONE_SIMULATOR
instead.
TARGET_OS_IPHONE is for code that might run on iOS or on MacOS X. Obviously you would want that code to behave the "iPhone" way on a simulator.
– gnasher729
Apr 5 '16 at 8:41
add a comment |
In swift :
#if (arch(i386) || arch(x86_64))
...
#endif
From Detect if app is being built for device or simulator in Swift
To distinguish between mac apps: #if ( arch( i386 ) || arch( x86_64 ) ) && !os( OSX ) // we’re on a simulator running on mac, and not a mac app. (For cross platforms code included in mac targets)
– Bobjt
Oct 21 '16 at 21:01
add a comment |
I had the same problem, both TARGET_IPHONE_SIMULATOR
and TARGET_OS_IPHONE
are always defined, and are set to 1. Pete's solution works, of course, but if you ever happen to build on something other than intel (unlikely, but who knows), here's something that's safe as long as the iphone hardware doesn't change (so your code will always work for the iphones currently out there):
#if defined __arm__ || defined __thumb__
#undef TARGET_IPHONE_SIMULATOR
#define TARGET_OS_IPHONE
#else
#define TARGET_IPHONE_SIMULATOR 1
#undef TARGET_OS_IPHONE
#endif
Put that somewhere convenient, and then pretend that the TARGET_*
constants were defined correctly.
add a comment |
Works for Swift 4
and Xcode 9.4.1
Use this code:
#if targetEnvironment(simulator)
// Simulator
#else
// Device
#endif
add a comment |
Has anyone considered the answer provided here?
I suppose the objective-c equivalent would be
+ (BOOL)isSimulator {
NSOperatingSystemVersion ios9 = {9, 0, 0};
NSProcessInfo *processInfo = [NSProcessInfo processInfo];
if ([processInfo isOperatingSystemAtLeastVersion:ios9]) {
NSDictionary<NSString *, NSString *> *environment = [processInfo environment];
NSString *simulator = [environment objectForKey:@"SIMULATOR_DEVICE_NAME"];
return simulator != nil;
} else {
UIDevice *currentDevice = [UIDevice currentDevice];
return ([currentDevice.model rangeOfString:@"Simulator"].location != NSNotFound);
}
}
add a comment |
To include all types of "simulators"
NSString *model = [[UIDevice currentDevice] model];
if([model rangeOfString:@"Simulator" options:NSCaseInsensitiveSearch].location !=NSNotFound)
{
// we are running in a simulator
}
3
Note that this doesn't work on Xcode 7 anymore!
– radex
Sep 9 '15 at 10:55
4
It has nothing to do with Xcode 7. If you run iOS Simulator with iOS8 (from Xcode 7) then this will work. It won't work for iOS9 where [[UIDevice currentDevice] model] returns only "iPhone" if app was launched from iOS Simulator
– tesla
Sep 23 '15 at 8:46
why not-[NSString containsString]
?
– Gobe
Oct 31 '16 at 23:23
add a comment |
With Swift 4.2 (Xcode 10), we can do this
#if targetEnvironment(simulator)
//simulator code
#else
#warning("Not compiling for simulator")
#endif
1
Just another copy paste
– J. Doe
Nov 9 '18 at 12:03
add a comment |
For Swift 4.2 / xCode 10
I created an extension on UIDevice, so I can easily ask for if the simulator is running.
// UIDevice+CheckSimulator.swift
import UIKit
extension UIDevice {
/// Checks if the current device that runs the app is xCode's simulator
static func isSimulator() -> Bool {
#if targetEnvironment(simulator)
return true
#else
return false
#endif
}
}
In my AppDelegate for example I use this method to decide wether registering for remote notification is necessary, which is not possible for the simulator.
// CHECK FOR REAL DEVICE / OR SIMULATOR
if UIDevice.isSimulator() == false {
// REGISTER FOR SILENT REMOTE NOTIFICATION
application.registerForRemoteNotifications()
}
add a comment |
My answer is based on @Daniel Magnusson answer and comments of @Nuthatch and @n.Drake. and I write it to save some time for swift users working on iOS9 and onwards.
This is what worked for me:
if UIDevice.currentDevice().name.hasSuffix("Simulator"){
//Code executing on Simulator
} else{
//Code executing on Device
}
1
The code won't work if a user addsSimulator
word in his device name
– mbelsky
May 9 '16 at 8:11
Unfortunately with XCode 8UIDevice.current.name
reports the name of the machine the Simulator is running on (typically something like "Simon's MacBook Pro" now) so the test has become unreliable. I am still looking into a clean way to fix it.
– Michael
Sep 26 '16 at 11:04
add a comment |
/// Returns true if its simulator and not a device
public static var isSimulator: Bool {
#if (arch(i386) || arch(x86_64)) && os(iOS)
return true
#else
return false
#endif
}
add a comment |
SWIFT 4 Solution
static let isSimulator: Bool = {
return TARGET_OS_SIMULATOR == 1
}()
TARGET_OS_SIMULATOR
is located in Darwin.TargetConditionals.swift
file.
add a comment |
Apple has added support for checking the app is targeted for the simulator with the following:
#if targetEnvironment(simulator)
let DEVICE_IS_SIMULATOR = true
#else
let DEVICE_IS_SIMULATOR = false
#endif
add a comment |
if nothing worked, try this
public struct Platform {
public static var isSimulator: Bool {
return TARGET_OS_SIMULATOR != 0 // Use this line in Xcode 7 or newer
}
}
add a comment |
In my opinion, the answer (presented above and repeated below):
NSString *model = [[UIDevice currentDevice] model];
if ([model isEqualToString:@"iPhone Simulator"]) {
//device is simulator
}
is the best answer because it is obviously executed at RUNTIME versus being a COMPILE DIRECTIVE.
11
I disagree. This code ends up in your product, whereas a compiler directive keeps the - on the device unnecessary - routine out.
– nine stones
Oct 24 '13 at 3:16
1
The compiler directives work because the device and simulators are completely different compile targets - ie you wouldn't use the same binary on both. It has to be compiled to different hardware, so it makes sense in that case.
– Brad Parks
Mar 12 '14 at 22:52
Being executed at RUNTIME makes it the worst possible answer.
– gnasher729
Apr 5 '16 at 8:43
add a comment |
This worked for me best
NSString *name = [[UIDevice currentDevice] name];
if ([name isEqualToString:@"iPhone Simulator"]) {
}
2
On Xcode 7.3, iPhone 6 Plus Simulator returns"iPhone"
.
– Eric
May 11 '16 at 10:32
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f458304%2fhow-can-i-programmatically-determine-if-my-app-is-running-in-the-iphone-simulato%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
22 Answers
22
active
oldest
votes
22 Answers
22
active
oldest
votes
active
oldest
votes
active
oldest
votes
Already asked, but with a very different title.
What #defines are set up by Xcode when compiling for iPhone
I'll repeat my answer from there:
It's in the SDK docs under "Compiling source code conditionally"
The relevant definition is TARGET_OS_SIMULATOR, which is defined in /usr/include/TargetConditionals.h within the iOS framework. On earlier versions of the toolchain, you had to write:
#include "TargetConditionals.h"
but this is no longer necessary on the current (Xcode 6/iOS8) toolchain.
So, for example, if you want to check that you are running on device, you should do
#if TARGET_OS_SIMULATOR
// Simulator-specific code
#else
// Device-specific code
#endif
depending on which is appropriate for your use-case.
1
Thanks. I agree with you this is a more specific version of your original question. If yours had come up in my original search, I wouldn't have even needed to ask.
– Jeffrey Meyer
Jan 19 '09 at 21:06
4
Be careful with these definitions. When you compile code with menu item 'Project > Set Active SDK > Simulator…', as TARGET_IPHONE_SIMULATOR as TARGET_OS_IPHONE variables are both defined! So the only right way to separate logic is pointed out below by Pete (Thanks dude).
– Vadim
Jan 24 '09 at 2:05
4
Watch the #if and #ifdef difference. For me it was the cause of incorrect behavior.
– Anton
Jan 9 '10 at 9:32
6
Perhaps the need to include TargetConditionals has been obviated since this was written, but just wanted to note that #if TARGET_IPHONE_SIMULATOR works without including TargetConditionals.h now.
– dmur
Mar 4 '14 at 23:34
2
how would this work in swift?
– Oren
Jul 31 '15 at 19:05
|
show 10 more comments
Already asked, but with a very different title.
What #defines are set up by Xcode when compiling for iPhone
I'll repeat my answer from there:
It's in the SDK docs under "Compiling source code conditionally"
The relevant definition is TARGET_OS_SIMULATOR, which is defined in /usr/include/TargetConditionals.h within the iOS framework. On earlier versions of the toolchain, you had to write:
#include "TargetConditionals.h"
but this is no longer necessary on the current (Xcode 6/iOS8) toolchain.
So, for example, if you want to check that you are running on device, you should do
#if TARGET_OS_SIMULATOR
// Simulator-specific code
#else
// Device-specific code
#endif
depending on which is appropriate for your use-case.
1
Thanks. I agree with you this is a more specific version of your original question. If yours had come up in my original search, I wouldn't have even needed to ask.
– Jeffrey Meyer
Jan 19 '09 at 21:06
4
Be careful with these definitions. When you compile code with menu item 'Project > Set Active SDK > Simulator…', as TARGET_IPHONE_SIMULATOR as TARGET_OS_IPHONE variables are both defined! So the only right way to separate logic is pointed out below by Pete (Thanks dude).
– Vadim
Jan 24 '09 at 2:05
4
Watch the #if and #ifdef difference. For me it was the cause of incorrect behavior.
– Anton
Jan 9 '10 at 9:32
6
Perhaps the need to include TargetConditionals has been obviated since this was written, but just wanted to note that #if TARGET_IPHONE_SIMULATOR works without including TargetConditionals.h now.
– dmur
Mar 4 '14 at 23:34
2
how would this work in swift?
– Oren
Jul 31 '15 at 19:05
|
show 10 more comments
Already asked, but with a very different title.
What #defines are set up by Xcode when compiling for iPhone
I'll repeat my answer from there:
It's in the SDK docs under "Compiling source code conditionally"
The relevant definition is TARGET_OS_SIMULATOR, which is defined in /usr/include/TargetConditionals.h within the iOS framework. On earlier versions of the toolchain, you had to write:
#include "TargetConditionals.h"
but this is no longer necessary on the current (Xcode 6/iOS8) toolchain.
So, for example, if you want to check that you are running on device, you should do
#if TARGET_OS_SIMULATOR
// Simulator-specific code
#else
// Device-specific code
#endif
depending on which is appropriate for your use-case.
Already asked, but with a very different title.
What #defines are set up by Xcode when compiling for iPhone
I'll repeat my answer from there:
It's in the SDK docs under "Compiling source code conditionally"
The relevant definition is TARGET_OS_SIMULATOR, which is defined in /usr/include/TargetConditionals.h within the iOS framework. On earlier versions of the toolchain, you had to write:
#include "TargetConditionals.h"
but this is no longer necessary on the current (Xcode 6/iOS8) toolchain.
So, for example, if you want to check that you are running on device, you should do
#if TARGET_OS_SIMULATOR
// Simulator-specific code
#else
// Device-specific code
#endif
depending on which is appropriate for your use-case.
edited Nov 6 '17 at 22:55
Jeremy Huddleston Sequoia
18.8k56075
18.8k56075
answered Jan 19 '09 at 17:17
Airsource LtdAirsource Ltd
23.9k136474
23.9k136474
1
Thanks. I agree with you this is a more specific version of your original question. If yours had come up in my original search, I wouldn't have even needed to ask.
– Jeffrey Meyer
Jan 19 '09 at 21:06
4
Be careful with these definitions. When you compile code with menu item 'Project > Set Active SDK > Simulator…', as TARGET_IPHONE_SIMULATOR as TARGET_OS_IPHONE variables are both defined! So the only right way to separate logic is pointed out below by Pete (Thanks dude).
– Vadim
Jan 24 '09 at 2:05
4
Watch the #if and #ifdef difference. For me it was the cause of incorrect behavior.
– Anton
Jan 9 '10 at 9:32
6
Perhaps the need to include TargetConditionals has been obviated since this was written, but just wanted to note that #if TARGET_IPHONE_SIMULATOR works without including TargetConditionals.h now.
– dmur
Mar 4 '14 at 23:34
2
how would this work in swift?
– Oren
Jul 31 '15 at 19:05
|
show 10 more comments
1
Thanks. I agree with you this is a more specific version of your original question. If yours had come up in my original search, I wouldn't have even needed to ask.
– Jeffrey Meyer
Jan 19 '09 at 21:06
4
Be careful with these definitions. When you compile code with menu item 'Project > Set Active SDK > Simulator…', as TARGET_IPHONE_SIMULATOR as TARGET_OS_IPHONE variables are both defined! So the only right way to separate logic is pointed out below by Pete (Thanks dude).
– Vadim
Jan 24 '09 at 2:05
4
Watch the #if and #ifdef difference. For me it was the cause of incorrect behavior.
– Anton
Jan 9 '10 at 9:32
6
Perhaps the need to include TargetConditionals has been obviated since this was written, but just wanted to note that #if TARGET_IPHONE_SIMULATOR works without including TargetConditionals.h now.
– dmur
Mar 4 '14 at 23:34
2
how would this work in swift?
– Oren
Jul 31 '15 at 19:05
1
1
Thanks. I agree with you this is a more specific version of your original question. If yours had come up in my original search, I wouldn't have even needed to ask.
– Jeffrey Meyer
Jan 19 '09 at 21:06
Thanks. I agree with you this is a more specific version of your original question. If yours had come up in my original search, I wouldn't have even needed to ask.
– Jeffrey Meyer
Jan 19 '09 at 21:06
4
4
Be careful with these definitions. When you compile code with menu item 'Project > Set Active SDK > Simulator…', as TARGET_IPHONE_SIMULATOR as TARGET_OS_IPHONE variables are both defined! So the only right way to separate logic is pointed out below by Pete (Thanks dude).
– Vadim
Jan 24 '09 at 2:05
Be careful with these definitions. When you compile code with menu item 'Project > Set Active SDK > Simulator…', as TARGET_IPHONE_SIMULATOR as TARGET_OS_IPHONE variables are both defined! So the only right way to separate logic is pointed out below by Pete (Thanks dude).
– Vadim
Jan 24 '09 at 2:05
4
4
Watch the #if and #ifdef difference. For me it was the cause of incorrect behavior.
– Anton
Jan 9 '10 at 9:32
Watch the #if and #ifdef difference. For me it was the cause of incorrect behavior.
– Anton
Jan 9 '10 at 9:32
6
6
Perhaps the need to include TargetConditionals has been obviated since this was written, but just wanted to note that #if TARGET_IPHONE_SIMULATOR works without including TargetConditionals.h now.
– dmur
Mar 4 '14 at 23:34
Perhaps the need to include TargetConditionals has been obviated since this was written, but just wanted to note that #if TARGET_IPHONE_SIMULATOR works without including TargetConditionals.h now.
– dmur
Mar 4 '14 at 23:34
2
2
how would this work in swift?
– Oren
Jul 31 '15 at 19:05
how would this work in swift?
– Oren
Jul 31 '15 at 19:05
|
show 10 more comments
Updated code:
This is purported to work officially.
#if TARGET_IPHONE_SIMULATOR
NSString *hello = @"Hello, iPhone simulator!";
#elif TARGET_OS_IPHONE
NSString *hello = @"Hello, device!";
#else
NSString *hello = @"Hello, unknown target!";
#endif
Original post (since deprecated)
This code will tell you if you are running in a simulator.
#ifdef __i386__
NSLog(@"Running in the simulator");
#else
NSLog(@"Running on a device");
#endif
7
As of iOS 8 and Xcode 6.1.1 the TARGET_OS_IPHONE is true on the simulator.
– malhal
Jan 11 '15 at 20:28
2
this doesn't worik anymore on newer XCode versions
– Fabio Napodano
Jan 25 '16 at 18:01
1
Unless you are in 2016 and run a 64 bit simulator. Or in 2019 and run your code on an iPhone with Intel processor.
– gnasher729
Apr 5 '16 at 8:40
add a comment |
Updated code:
This is purported to work officially.
#if TARGET_IPHONE_SIMULATOR
NSString *hello = @"Hello, iPhone simulator!";
#elif TARGET_OS_IPHONE
NSString *hello = @"Hello, device!";
#else
NSString *hello = @"Hello, unknown target!";
#endif
Original post (since deprecated)
This code will tell you if you are running in a simulator.
#ifdef __i386__
NSLog(@"Running in the simulator");
#else
NSLog(@"Running on a device");
#endif
7
As of iOS 8 and Xcode 6.1.1 the TARGET_OS_IPHONE is true on the simulator.
– malhal
Jan 11 '15 at 20:28
2
this doesn't worik anymore on newer XCode versions
– Fabio Napodano
Jan 25 '16 at 18:01
1
Unless you are in 2016 and run a 64 bit simulator. Or in 2019 and run your code on an iPhone with Intel processor.
– gnasher729
Apr 5 '16 at 8:40
add a comment |
Updated code:
This is purported to work officially.
#if TARGET_IPHONE_SIMULATOR
NSString *hello = @"Hello, iPhone simulator!";
#elif TARGET_OS_IPHONE
NSString *hello = @"Hello, device!";
#else
NSString *hello = @"Hello, unknown target!";
#endif
Original post (since deprecated)
This code will tell you if you are running in a simulator.
#ifdef __i386__
NSLog(@"Running in the simulator");
#else
NSLog(@"Running on a device");
#endif
Updated code:
This is purported to work officially.
#if TARGET_IPHONE_SIMULATOR
NSString *hello = @"Hello, iPhone simulator!";
#elif TARGET_OS_IPHONE
NSString *hello = @"Hello, device!";
#else
NSString *hello = @"Hello, unknown target!";
#endif
Original post (since deprecated)
This code will tell you if you are running in a simulator.
#ifdef __i386__
NSLog(@"Running in the simulator");
#else
NSLog(@"Running on a device");
#endif
edited Aug 14 '16 at 5:18
Albert Renshaw
9,2121368152
9,2121368152
answered Jan 19 '09 at 17:10
PetePete
3,45522426
3,45522426
7
As of iOS 8 and Xcode 6.1.1 the TARGET_OS_IPHONE is true on the simulator.
– malhal
Jan 11 '15 at 20:28
2
this doesn't worik anymore on newer XCode versions
– Fabio Napodano
Jan 25 '16 at 18:01
1
Unless you are in 2016 and run a 64 bit simulator. Or in 2019 and run your code on an iPhone with Intel processor.
– gnasher729
Apr 5 '16 at 8:40
add a comment |
7
As of iOS 8 and Xcode 6.1.1 the TARGET_OS_IPHONE is true on the simulator.
– malhal
Jan 11 '15 at 20:28
2
this doesn't worik anymore on newer XCode versions
– Fabio Napodano
Jan 25 '16 at 18:01
1
Unless you are in 2016 and run a 64 bit simulator. Or in 2019 and run your code on an iPhone with Intel processor.
– gnasher729
Apr 5 '16 at 8:40
7
7
As of iOS 8 and Xcode 6.1.1 the TARGET_OS_IPHONE is true on the simulator.
– malhal
Jan 11 '15 at 20:28
As of iOS 8 and Xcode 6.1.1 the TARGET_OS_IPHONE is true on the simulator.
– malhal
Jan 11 '15 at 20:28
2
2
this doesn't worik anymore on newer XCode versions
– Fabio Napodano
Jan 25 '16 at 18:01
this doesn't worik anymore on newer XCode versions
– Fabio Napodano
Jan 25 '16 at 18:01
1
1
Unless you are in 2016 and run a 64 bit simulator. Or in 2019 and run your code on an iPhone with Intel processor.
– gnasher729
Apr 5 '16 at 8:40
Unless you are in 2016 and run a 64 bit simulator. Or in 2019 and run your code on an iPhone with Intel processor.
– gnasher729
Apr 5 '16 at 8:40
add a comment |
Not pre-processor directive, but this was what I was looking for when i came to this question;
NSString *model = [[UIDevice currentDevice] model];
if ([model isEqualToString:@"iPhone Simulator"]) {
//device is simulator
}
9
[model compare:iPhoneSimulator] == NSOrderedSame
should be written as[model isEqualToString:iPhoneSimulator]
– user102008
Jan 11 '11 at 22:51
17
Or[model hasSuffix:@"Simulator"]
if you only care about "simulator" in general, not iPhone or iPad in particular. This answer won't work for iPad simulator :)
– Nuthatch
Aug 12 '14 at 15:37
Upvoted because Nuthatch's comment makes this the best answer in toto.
– Le Mot Juiced
Apr 9 '15 at 19:52
9
No longer works on Simulator for iOS9!
– KlimczakM
Oct 16 '15 at 10:17
11
In iOS9, check the devicename
instead ofmodel
– n.Drake
Oct 29 '15 at 8:51
|
show 1 more comment
Not pre-processor directive, but this was what I was looking for when i came to this question;
NSString *model = [[UIDevice currentDevice] model];
if ([model isEqualToString:@"iPhone Simulator"]) {
//device is simulator
}
9
[model compare:iPhoneSimulator] == NSOrderedSame
should be written as[model isEqualToString:iPhoneSimulator]
– user102008
Jan 11 '11 at 22:51
17
Or[model hasSuffix:@"Simulator"]
if you only care about "simulator" in general, not iPhone or iPad in particular. This answer won't work for iPad simulator :)
– Nuthatch
Aug 12 '14 at 15:37
Upvoted because Nuthatch's comment makes this the best answer in toto.
– Le Mot Juiced
Apr 9 '15 at 19:52
9
No longer works on Simulator for iOS9!
– KlimczakM
Oct 16 '15 at 10:17
11
In iOS9, check the devicename
instead ofmodel
– n.Drake
Oct 29 '15 at 8:51
|
show 1 more comment
Not pre-processor directive, but this was what I was looking for when i came to this question;
NSString *model = [[UIDevice currentDevice] model];
if ([model isEqualToString:@"iPhone Simulator"]) {
//device is simulator
}
Not pre-processor directive, but this was what I was looking for when i came to this question;
NSString *model = [[UIDevice currentDevice] model];
if ([model isEqualToString:@"iPhone Simulator"]) {
//device is simulator
}
edited Sep 9 '11 at 21:57
gerry3
20.2k85973
20.2k85973
answered Aug 18 '10 at 10:35
Daniel MagnussonDaniel Magnusson
7,49723342
7,49723342
9
[model compare:iPhoneSimulator] == NSOrderedSame
should be written as[model isEqualToString:iPhoneSimulator]
– user102008
Jan 11 '11 at 22:51
17
Or[model hasSuffix:@"Simulator"]
if you only care about "simulator" in general, not iPhone or iPad in particular. This answer won't work for iPad simulator :)
– Nuthatch
Aug 12 '14 at 15:37
Upvoted because Nuthatch's comment makes this the best answer in toto.
– Le Mot Juiced
Apr 9 '15 at 19:52
9
No longer works on Simulator for iOS9!
– KlimczakM
Oct 16 '15 at 10:17
11
In iOS9, check the devicename
instead ofmodel
– n.Drake
Oct 29 '15 at 8:51
|
show 1 more comment
9
[model compare:iPhoneSimulator] == NSOrderedSame
should be written as[model isEqualToString:iPhoneSimulator]
– user102008
Jan 11 '11 at 22:51
17
Or[model hasSuffix:@"Simulator"]
if you only care about "simulator" in general, not iPhone or iPad in particular. This answer won't work for iPad simulator :)
– Nuthatch
Aug 12 '14 at 15:37
Upvoted because Nuthatch's comment makes this the best answer in toto.
– Le Mot Juiced
Apr 9 '15 at 19:52
9
No longer works on Simulator for iOS9!
– KlimczakM
Oct 16 '15 at 10:17
11
In iOS9, check the devicename
instead ofmodel
– n.Drake
Oct 29 '15 at 8:51
9
9
[model compare:iPhoneSimulator] == NSOrderedSame
should be written as [model isEqualToString:iPhoneSimulator]
– user102008
Jan 11 '11 at 22:51
[model compare:iPhoneSimulator] == NSOrderedSame
should be written as [model isEqualToString:iPhoneSimulator]
– user102008
Jan 11 '11 at 22:51
17
17
Or
[model hasSuffix:@"Simulator"]
if you only care about "simulator" in general, not iPhone or iPad in particular. This answer won't work for iPad simulator :)– Nuthatch
Aug 12 '14 at 15:37
Or
[model hasSuffix:@"Simulator"]
if you only care about "simulator" in general, not iPhone or iPad in particular. This answer won't work for iPad simulator :)– Nuthatch
Aug 12 '14 at 15:37
Upvoted because Nuthatch's comment makes this the best answer in toto.
– Le Mot Juiced
Apr 9 '15 at 19:52
Upvoted because Nuthatch's comment makes this the best answer in toto.
– Le Mot Juiced
Apr 9 '15 at 19:52
9
9
No longer works on Simulator for iOS9!
– KlimczakM
Oct 16 '15 at 10:17
No longer works on Simulator for iOS9!
– KlimczakM
Oct 16 '15 at 10:17
11
11
In iOS9, check the device
name
instead of model
– n.Drake
Oct 29 '15 at 8:51
In iOS9, check the device
name
instead of model
– n.Drake
Oct 29 '15 at 8:51
|
show 1 more comment
The best way to do this is:
#if TARGET_IPHONE_SIMULATOR
and not
#ifdef TARGET_IPHONE_SIMULATOR
since its always defined: 0 or 1
add a comment |
The best way to do this is:
#if TARGET_IPHONE_SIMULATOR
and not
#ifdef TARGET_IPHONE_SIMULATOR
since its always defined: 0 or 1
add a comment |
The best way to do this is:
#if TARGET_IPHONE_SIMULATOR
and not
#ifdef TARGET_IPHONE_SIMULATOR
since its always defined: 0 or 1
The best way to do this is:
#if TARGET_IPHONE_SIMULATOR
and not
#ifdef TARGET_IPHONE_SIMULATOR
since its always defined: 0 or 1
answered Mar 7 '11 at 7:05
TaranfxTaranfx
5,759126991
5,759126991
add a comment |
add a comment |
In case of Swift we can implement following
We can create struct which allows you to create a structured data
struct Platform {
static let isSimulator: Bool = {
#if arch(i386) || arch(x86_64)
return true
#endif
return false
}()
}
Then If we wanted to Detect if app is being built for device or simulator in Swift then .
if Platform.isSimulator {
// Do one thing
}
else {
// Do the other
}
Cleanest implementation in my opinion, and it accounts for x86_64 and i386 architectures. Helped me overcome a weird device vs. simulator bug in Core Data. You're the man!
– Iron John Bonney
May 26 '16 at 22:01
thanks for the complement dude
– Nischal Hada
May 27 '16 at 8:35
Really cool answer.
– DawnSong
Nov 9 '17 at 9:47
thanks buddy @Dawn Song
– Nischal Hada
Nov 10 '17 at 6:19
5
In Playground, you will get a warning, "Code after 'return' will never be executed". So I think#if #else #endif
will be better.
– DawnSong
Nov 10 '17 at 8:40
add a comment |
In case of Swift we can implement following
We can create struct which allows you to create a structured data
struct Platform {
static let isSimulator: Bool = {
#if arch(i386) || arch(x86_64)
return true
#endif
return false
}()
}
Then If we wanted to Detect if app is being built for device or simulator in Swift then .
if Platform.isSimulator {
// Do one thing
}
else {
// Do the other
}
Cleanest implementation in my opinion, and it accounts for x86_64 and i386 architectures. Helped me overcome a weird device vs. simulator bug in Core Data. You're the man!
– Iron John Bonney
May 26 '16 at 22:01
thanks for the complement dude
– Nischal Hada
May 27 '16 at 8:35
Really cool answer.
– DawnSong
Nov 9 '17 at 9:47
thanks buddy @Dawn Song
– Nischal Hada
Nov 10 '17 at 6:19
5
In Playground, you will get a warning, "Code after 'return' will never be executed". So I think#if #else #endif
will be better.
– DawnSong
Nov 10 '17 at 8:40
add a comment |
In case of Swift we can implement following
We can create struct which allows you to create a structured data
struct Platform {
static let isSimulator: Bool = {
#if arch(i386) || arch(x86_64)
return true
#endif
return false
}()
}
Then If we wanted to Detect if app is being built for device or simulator in Swift then .
if Platform.isSimulator {
// Do one thing
}
else {
// Do the other
}
In case of Swift we can implement following
We can create struct which allows you to create a structured data
struct Platform {
static let isSimulator: Bool = {
#if arch(i386) || arch(x86_64)
return true
#endif
return false
}()
}
Then If we wanted to Detect if app is being built for device or simulator in Swift then .
if Platform.isSimulator {
// Do one thing
}
else {
// Do the other
}
edited Aug 29 '17 at 7:37
Nikolay Shubenkov
2,49612027
2,49612027
answered Feb 25 '16 at 4:48
Nischal HadaNischal Hada
2,19712047
2,19712047
Cleanest implementation in my opinion, and it accounts for x86_64 and i386 architectures. Helped me overcome a weird device vs. simulator bug in Core Data. You're the man!
– Iron John Bonney
May 26 '16 at 22:01
thanks for the complement dude
– Nischal Hada
May 27 '16 at 8:35
Really cool answer.
– DawnSong
Nov 9 '17 at 9:47
thanks buddy @Dawn Song
– Nischal Hada
Nov 10 '17 at 6:19
5
In Playground, you will get a warning, "Code after 'return' will never be executed". So I think#if #else #endif
will be better.
– DawnSong
Nov 10 '17 at 8:40
add a comment |
Cleanest implementation in my opinion, and it accounts for x86_64 and i386 architectures. Helped me overcome a weird device vs. simulator bug in Core Data. You're the man!
– Iron John Bonney
May 26 '16 at 22:01
thanks for the complement dude
– Nischal Hada
May 27 '16 at 8:35
Really cool answer.
– DawnSong
Nov 9 '17 at 9:47
thanks buddy @Dawn Song
– Nischal Hada
Nov 10 '17 at 6:19
5
In Playground, you will get a warning, "Code after 'return' will never be executed". So I think#if #else #endif
will be better.
– DawnSong
Nov 10 '17 at 8:40
Cleanest implementation in my opinion, and it accounts for x86_64 and i386 architectures. Helped me overcome a weird device vs. simulator bug in Core Data. You're the man!
– Iron John Bonney
May 26 '16 at 22:01
Cleanest implementation in my opinion, and it accounts for x86_64 and i386 architectures. Helped me overcome a weird device vs. simulator bug in Core Data. You're the man!
– Iron John Bonney
May 26 '16 at 22:01
thanks for the complement dude
– Nischal Hada
May 27 '16 at 8:35
thanks for the complement dude
– Nischal Hada
May 27 '16 at 8:35
Really cool answer.
– DawnSong
Nov 9 '17 at 9:47
Really cool answer.
– DawnSong
Nov 9 '17 at 9:47
thanks buddy @Dawn Song
– Nischal Hada
Nov 10 '17 at 6:19
thanks buddy @Dawn Song
– Nischal Hada
Nov 10 '17 at 6:19
5
5
In Playground, you will get a warning, "Code after 'return' will never be executed". So I think
#if #else #endif
will be better.– DawnSong
Nov 10 '17 at 8:40
In Playground, you will get a warning, "Code after 'return' will never be executed". So I think
#if #else #endif
will be better.– DawnSong
Nov 10 '17 at 8:40
add a comment |
THERE IS A BETTER WAY NOW!
As of Xcode 9.3 beta 4 you can use #if targetEnvironment(simulator)
to check.
#if targetEnvironment(simulator)
//Your simulator code
#endif
UPDATE
Xcode 10 and iOS 12 SDK supports this too.
3
This should be the new accepted answer.
– Dan Loewenherz
Apr 4 '18 at 19:03
This is the only that works for me, rest of the solutions didn't work.
– Vrutin Rathod
May 17 '18 at 15:59
this proposal has been accepted in swift 4.1
– Hamsternik
Jun 15 '18 at 13:22
add a comment |
THERE IS A BETTER WAY NOW!
As of Xcode 9.3 beta 4 you can use #if targetEnvironment(simulator)
to check.
#if targetEnvironment(simulator)
//Your simulator code
#endif
UPDATE
Xcode 10 and iOS 12 SDK supports this too.
3
This should be the new accepted answer.
– Dan Loewenherz
Apr 4 '18 at 19:03
This is the only that works for me, rest of the solutions didn't work.
– Vrutin Rathod
May 17 '18 at 15:59
this proposal has been accepted in swift 4.1
– Hamsternik
Jun 15 '18 at 13:22
add a comment |
THERE IS A BETTER WAY NOW!
As of Xcode 9.3 beta 4 you can use #if targetEnvironment(simulator)
to check.
#if targetEnvironment(simulator)
//Your simulator code
#endif
UPDATE
Xcode 10 and iOS 12 SDK supports this too.
THERE IS A BETTER WAY NOW!
As of Xcode 9.3 beta 4 you can use #if targetEnvironment(simulator)
to check.
#if targetEnvironment(simulator)
//Your simulator code
#endif
UPDATE
Xcode 10 and iOS 12 SDK supports this too.
edited Sep 20 '18 at 15:01
answered Mar 12 '18 at 13:50
Stefan VasiljevicStefan Vasiljevic
2,67111215
2,67111215
3
This should be the new accepted answer.
– Dan Loewenherz
Apr 4 '18 at 19:03
This is the only that works for me, rest of the solutions didn't work.
– Vrutin Rathod
May 17 '18 at 15:59
this proposal has been accepted in swift 4.1
– Hamsternik
Jun 15 '18 at 13:22
add a comment |
3
This should be the new accepted answer.
– Dan Loewenherz
Apr 4 '18 at 19:03
This is the only that works for me, rest of the solutions didn't work.
– Vrutin Rathod
May 17 '18 at 15:59
this proposal has been accepted in swift 4.1
– Hamsternik
Jun 15 '18 at 13:22
3
3
This should be the new accepted answer.
– Dan Loewenherz
Apr 4 '18 at 19:03
This should be the new accepted answer.
– Dan Loewenherz
Apr 4 '18 at 19:03
This is the only that works for me, rest of the solutions didn't work.
– Vrutin Rathod
May 17 '18 at 15:59
This is the only that works for me, rest of the solutions didn't work.
– Vrutin Rathod
May 17 '18 at 15:59
this proposal has been accepted in swift 4.1
– Hamsternik
Jun 15 '18 at 13:22
this proposal has been accepted in swift 4.1
– Hamsternik
Jun 15 '18 at 13:22
add a comment |
All those answer are good, but it somehow confuses newbie like me as it does not clarify compile check and runtime check. Preprocessor are before compile time, but we should make it clearer
This blog article shows How to detect the iPhone simulator? clearly
Runtime
First of all, let’s shortly discuss. UIDevice provides you already information about the device
[[UIDevice currentDevice] model]
will return you “iPhone Simulator” or “iPhone” according to where the app is running.
Compile time
However what you want is to use compile time defines. Why? Because you compile your app strictly to be run either inside the Simulator or on the device. Apple makes a define called TARGET_IPHONE_SIMULATOR
. So let’s look at the code :
#if TARGET_IPHONE_SIMULATOR
NSLog(@"Running in Simulator - no app store or giro");
#endif
1
How does this improve on other answers?
– Mark
Jun 17 '14 at 16:42
@Mark It clarifies a little bit
– onmyway133
Jun 18 '14 at 16:52
5
Currently, in Xcode 7, iOS 9 Simulator[[UIDevice currentDevice] model]
is returningiPhone
also instead ofiPhone Simulator
. So, I think this is not the best approach.
– eMdOS
Jan 12 '16 at 19:11
add a comment |
All those answer are good, but it somehow confuses newbie like me as it does not clarify compile check and runtime check. Preprocessor are before compile time, but we should make it clearer
This blog article shows How to detect the iPhone simulator? clearly
Runtime
First of all, let’s shortly discuss. UIDevice provides you already information about the device
[[UIDevice currentDevice] model]
will return you “iPhone Simulator” or “iPhone” according to where the app is running.
Compile time
However what you want is to use compile time defines. Why? Because you compile your app strictly to be run either inside the Simulator or on the device. Apple makes a define called TARGET_IPHONE_SIMULATOR
. So let’s look at the code :
#if TARGET_IPHONE_SIMULATOR
NSLog(@"Running in Simulator - no app store or giro");
#endif
1
How does this improve on other answers?
– Mark
Jun 17 '14 at 16:42
@Mark It clarifies a little bit
– onmyway133
Jun 18 '14 at 16:52
5
Currently, in Xcode 7, iOS 9 Simulator[[UIDevice currentDevice] model]
is returningiPhone
also instead ofiPhone Simulator
. So, I think this is not the best approach.
– eMdOS
Jan 12 '16 at 19:11
add a comment |
All those answer are good, but it somehow confuses newbie like me as it does not clarify compile check and runtime check. Preprocessor are before compile time, but we should make it clearer
This blog article shows How to detect the iPhone simulator? clearly
Runtime
First of all, let’s shortly discuss. UIDevice provides you already information about the device
[[UIDevice currentDevice] model]
will return you “iPhone Simulator” or “iPhone” according to where the app is running.
Compile time
However what you want is to use compile time defines. Why? Because you compile your app strictly to be run either inside the Simulator or on the device. Apple makes a define called TARGET_IPHONE_SIMULATOR
. So let’s look at the code :
#if TARGET_IPHONE_SIMULATOR
NSLog(@"Running in Simulator - no app store or giro");
#endif
All those answer are good, but it somehow confuses newbie like me as it does not clarify compile check and runtime check. Preprocessor are before compile time, but we should make it clearer
This blog article shows How to detect the iPhone simulator? clearly
Runtime
First of all, let’s shortly discuss. UIDevice provides you already information about the device
[[UIDevice currentDevice] model]
will return you “iPhone Simulator” or “iPhone” according to where the app is running.
Compile time
However what you want is to use compile time defines. Why? Because you compile your app strictly to be run either inside the Simulator or on the device. Apple makes a define called TARGET_IPHONE_SIMULATOR
. So let’s look at the code :
#if TARGET_IPHONE_SIMULATOR
NSLog(@"Running in Simulator - no app store or giro");
#endif
edited Jun 17 '14 at 16:38
answered Jun 17 '14 at 2:57
onmyway133onmyway133
24.9k15162192
24.9k15162192
1
How does this improve on other answers?
– Mark
Jun 17 '14 at 16:42
@Mark It clarifies a little bit
– onmyway133
Jun 18 '14 at 16:52
5
Currently, in Xcode 7, iOS 9 Simulator[[UIDevice currentDevice] model]
is returningiPhone
also instead ofiPhone Simulator
. So, I think this is not the best approach.
– eMdOS
Jan 12 '16 at 19:11
add a comment |
1
How does this improve on other answers?
– Mark
Jun 17 '14 at 16:42
@Mark It clarifies a little bit
– onmyway133
Jun 18 '14 at 16:52
5
Currently, in Xcode 7, iOS 9 Simulator[[UIDevice currentDevice] model]
is returningiPhone
also instead ofiPhone Simulator
. So, I think this is not the best approach.
– eMdOS
Jan 12 '16 at 19:11
1
1
How does this improve on other answers?
– Mark
Jun 17 '14 at 16:42
How does this improve on other answers?
– Mark
Jun 17 '14 at 16:42
@Mark It clarifies a little bit
– onmyway133
Jun 18 '14 at 16:52
@Mark It clarifies a little bit
– onmyway133
Jun 18 '14 at 16:52
5
5
Currently, in Xcode 7, iOS 9 Simulator
[[UIDevice currentDevice] model]
is returning iPhone
also instead of iPhone Simulator
. So, I think this is not the best approach.– eMdOS
Jan 12 '16 at 19:11
Currently, in Xcode 7, iOS 9 Simulator
[[UIDevice currentDevice] model]
is returning iPhone
also instead of iPhone Simulator
. So, I think this is not the best approach.– eMdOS
Jan 12 '16 at 19:11
add a comment |
The previous answers are a little dated. I found that all you need to do is query the TARGET_IPHONE_SIMULATOR
macro (no need to include any other header files [assuming you are coding for iOS]).
I attempted TARGET_OS_IPHONE
but it returned the same value (1) when running on an actual device and simulator, that's why I recommend using TARGET_IPHONE_SIMULATOR
instead.
TARGET_OS_IPHONE is for code that might run on iOS or on MacOS X. Obviously you would want that code to behave the "iPhone" way on a simulator.
– gnasher729
Apr 5 '16 at 8:41
add a comment |
The previous answers are a little dated. I found that all you need to do is query the TARGET_IPHONE_SIMULATOR
macro (no need to include any other header files [assuming you are coding for iOS]).
I attempted TARGET_OS_IPHONE
but it returned the same value (1) when running on an actual device and simulator, that's why I recommend using TARGET_IPHONE_SIMULATOR
instead.
TARGET_OS_IPHONE is for code that might run on iOS or on MacOS X. Obviously you would want that code to behave the "iPhone" way on a simulator.
– gnasher729
Apr 5 '16 at 8:41
add a comment |
The previous answers are a little dated. I found that all you need to do is query the TARGET_IPHONE_SIMULATOR
macro (no need to include any other header files [assuming you are coding for iOS]).
I attempted TARGET_OS_IPHONE
but it returned the same value (1) when running on an actual device and simulator, that's why I recommend using TARGET_IPHONE_SIMULATOR
instead.
The previous answers are a little dated. I found that all you need to do is query the TARGET_IPHONE_SIMULATOR
macro (no need to include any other header files [assuming you are coding for iOS]).
I attempted TARGET_OS_IPHONE
but it returned the same value (1) when running on an actual device and simulator, that's why I recommend using TARGET_IPHONE_SIMULATOR
instead.
answered May 21 '13 at 7:10
StunnerStunner
7,8451069123
7,8451069123
TARGET_OS_IPHONE is for code that might run on iOS or on MacOS X. Obviously you would want that code to behave the "iPhone" way on a simulator.
– gnasher729
Apr 5 '16 at 8:41
add a comment |
TARGET_OS_IPHONE is for code that might run on iOS or on MacOS X. Obviously you would want that code to behave the "iPhone" way on a simulator.
– gnasher729
Apr 5 '16 at 8:41
TARGET_OS_IPHONE is for code that might run on iOS or on MacOS X. Obviously you would want that code to behave the "iPhone" way on a simulator.
– gnasher729
Apr 5 '16 at 8:41
TARGET_OS_IPHONE is for code that might run on iOS or on MacOS X. Obviously you would want that code to behave the "iPhone" way on a simulator.
– gnasher729
Apr 5 '16 at 8:41
add a comment |
In swift :
#if (arch(i386) || arch(x86_64))
...
#endif
From Detect if app is being built for device or simulator in Swift
To distinguish between mac apps: #if ( arch( i386 ) || arch( x86_64 ) ) && !os( OSX ) // we’re on a simulator running on mac, and not a mac app. (For cross platforms code included in mac targets)
– Bobjt
Oct 21 '16 at 21:01
add a comment |
In swift :
#if (arch(i386) || arch(x86_64))
...
#endif
From Detect if app is being built for device or simulator in Swift
To distinguish between mac apps: #if ( arch( i386 ) || arch( x86_64 ) ) && !os( OSX ) // we’re on a simulator running on mac, and not a mac app. (For cross platforms code included in mac targets)
– Bobjt
Oct 21 '16 at 21:01
add a comment |
In swift :
#if (arch(i386) || arch(x86_64))
...
#endif
From Detect if app is being built for device or simulator in Swift
In swift :
#if (arch(i386) || arch(x86_64))
...
#endif
From Detect if app is being built for device or simulator in Swift
edited May 23 '17 at 12:10
Community♦
11
11
answered Jun 16 '16 at 14:10
CedricSoubrieCedricSoubrie
5,91123242
5,91123242
To distinguish between mac apps: #if ( arch( i386 ) || arch( x86_64 ) ) && !os( OSX ) // we’re on a simulator running on mac, and not a mac app. (For cross platforms code included in mac targets)
– Bobjt
Oct 21 '16 at 21:01
add a comment |
To distinguish between mac apps: #if ( arch( i386 ) || arch( x86_64 ) ) && !os( OSX ) // we’re on a simulator running on mac, and not a mac app. (For cross platforms code included in mac targets)
– Bobjt
Oct 21 '16 at 21:01
To distinguish between mac apps: #if ( arch( i386 ) || arch( x86_64 ) ) && !os( OSX ) // we’re on a simulator running on mac, and not a mac app. (For cross platforms code included in mac targets)
– Bobjt
Oct 21 '16 at 21:01
To distinguish between mac apps: #if ( arch( i386 ) || arch( x86_64 ) ) && !os( OSX ) // we’re on a simulator running on mac, and not a mac app. (For cross platforms code included in mac targets)
– Bobjt
Oct 21 '16 at 21:01
add a comment |
I had the same problem, both TARGET_IPHONE_SIMULATOR
and TARGET_OS_IPHONE
are always defined, and are set to 1. Pete's solution works, of course, but if you ever happen to build on something other than intel (unlikely, but who knows), here's something that's safe as long as the iphone hardware doesn't change (so your code will always work for the iphones currently out there):
#if defined __arm__ || defined __thumb__
#undef TARGET_IPHONE_SIMULATOR
#define TARGET_OS_IPHONE
#else
#define TARGET_IPHONE_SIMULATOR 1
#undef TARGET_OS_IPHONE
#endif
Put that somewhere convenient, and then pretend that the TARGET_*
constants were defined correctly.
add a comment |
I had the same problem, both TARGET_IPHONE_SIMULATOR
and TARGET_OS_IPHONE
are always defined, and are set to 1. Pete's solution works, of course, but if you ever happen to build on something other than intel (unlikely, but who knows), here's something that's safe as long as the iphone hardware doesn't change (so your code will always work for the iphones currently out there):
#if defined __arm__ || defined __thumb__
#undef TARGET_IPHONE_SIMULATOR
#define TARGET_OS_IPHONE
#else
#define TARGET_IPHONE_SIMULATOR 1
#undef TARGET_OS_IPHONE
#endif
Put that somewhere convenient, and then pretend that the TARGET_*
constants were defined correctly.
add a comment |
I had the same problem, both TARGET_IPHONE_SIMULATOR
and TARGET_OS_IPHONE
are always defined, and are set to 1. Pete's solution works, of course, but if you ever happen to build on something other than intel (unlikely, but who knows), here's something that's safe as long as the iphone hardware doesn't change (so your code will always work for the iphones currently out there):
#if defined __arm__ || defined __thumb__
#undef TARGET_IPHONE_SIMULATOR
#define TARGET_OS_IPHONE
#else
#define TARGET_IPHONE_SIMULATOR 1
#undef TARGET_OS_IPHONE
#endif
Put that somewhere convenient, and then pretend that the TARGET_*
constants were defined correctly.
I had the same problem, both TARGET_IPHONE_SIMULATOR
and TARGET_OS_IPHONE
are always defined, and are set to 1. Pete's solution works, of course, but if you ever happen to build on something other than intel (unlikely, but who knows), here's something that's safe as long as the iphone hardware doesn't change (so your code will always work for the iphones currently out there):
#if defined __arm__ || defined __thumb__
#undef TARGET_IPHONE_SIMULATOR
#define TARGET_OS_IPHONE
#else
#define TARGET_IPHONE_SIMULATOR 1
#undef TARGET_OS_IPHONE
#endif
Put that somewhere convenient, and then pretend that the TARGET_*
constants were defined correctly.
answered Jun 16 '09 at 17:48
Martin
add a comment |
add a comment |
Works for Swift 4
and Xcode 9.4.1
Use this code:
#if targetEnvironment(simulator)
// Simulator
#else
// Device
#endif
add a comment |
Works for Swift 4
and Xcode 9.4.1
Use this code:
#if targetEnvironment(simulator)
// Simulator
#else
// Device
#endif
add a comment |
Works for Swift 4
and Xcode 9.4.1
Use this code:
#if targetEnvironment(simulator)
// Simulator
#else
// Device
#endif
Works for Swift 4
and Xcode 9.4.1
Use this code:
#if targetEnvironment(simulator)
// Simulator
#else
// Device
#endif
answered Jul 18 '18 at 19:06
Haroldo GondimHaroldo Gondim
3,08582650
3,08582650
add a comment |
add a comment |
Has anyone considered the answer provided here?
I suppose the objective-c equivalent would be
+ (BOOL)isSimulator {
NSOperatingSystemVersion ios9 = {9, 0, 0};
NSProcessInfo *processInfo = [NSProcessInfo processInfo];
if ([processInfo isOperatingSystemAtLeastVersion:ios9]) {
NSDictionary<NSString *, NSString *> *environment = [processInfo environment];
NSString *simulator = [environment objectForKey:@"SIMULATOR_DEVICE_NAME"];
return simulator != nil;
} else {
UIDevice *currentDevice = [UIDevice currentDevice];
return ([currentDevice.model rangeOfString:@"Simulator"].location != NSNotFound);
}
}
add a comment |
Has anyone considered the answer provided here?
I suppose the objective-c equivalent would be
+ (BOOL)isSimulator {
NSOperatingSystemVersion ios9 = {9, 0, 0};
NSProcessInfo *processInfo = [NSProcessInfo processInfo];
if ([processInfo isOperatingSystemAtLeastVersion:ios9]) {
NSDictionary<NSString *, NSString *> *environment = [processInfo environment];
NSString *simulator = [environment objectForKey:@"SIMULATOR_DEVICE_NAME"];
return simulator != nil;
} else {
UIDevice *currentDevice = [UIDevice currentDevice];
return ([currentDevice.model rangeOfString:@"Simulator"].location != NSNotFound);
}
}
add a comment |
Has anyone considered the answer provided here?
I suppose the objective-c equivalent would be
+ (BOOL)isSimulator {
NSOperatingSystemVersion ios9 = {9, 0, 0};
NSProcessInfo *processInfo = [NSProcessInfo processInfo];
if ([processInfo isOperatingSystemAtLeastVersion:ios9]) {
NSDictionary<NSString *, NSString *> *environment = [processInfo environment];
NSString *simulator = [environment objectForKey:@"SIMULATOR_DEVICE_NAME"];
return simulator != nil;
} else {
UIDevice *currentDevice = [UIDevice currentDevice];
return ([currentDevice.model rangeOfString:@"Simulator"].location != NSNotFound);
}
}
Has anyone considered the answer provided here?
I suppose the objective-c equivalent would be
+ (BOOL)isSimulator {
NSOperatingSystemVersion ios9 = {9, 0, 0};
NSProcessInfo *processInfo = [NSProcessInfo processInfo];
if ([processInfo isOperatingSystemAtLeastVersion:ios9]) {
NSDictionary<NSString *, NSString *> *environment = [processInfo environment];
NSString *simulator = [environment objectForKey:@"SIMULATOR_DEVICE_NAME"];
return simulator != nil;
} else {
UIDevice *currentDevice = [UIDevice currentDevice];
return ([currentDevice.model rangeOfString:@"Simulator"].location != NSNotFound);
}
}
answered Jul 26 '17 at 13:59
Vijay SharmaVijay Sharma
1,49711217
1,49711217
add a comment |
add a comment |
To include all types of "simulators"
NSString *model = [[UIDevice currentDevice] model];
if([model rangeOfString:@"Simulator" options:NSCaseInsensitiveSearch].location !=NSNotFound)
{
// we are running in a simulator
}
3
Note that this doesn't work on Xcode 7 anymore!
– radex
Sep 9 '15 at 10:55
4
It has nothing to do with Xcode 7. If you run iOS Simulator with iOS8 (from Xcode 7) then this will work. It won't work for iOS9 where [[UIDevice currentDevice] model] returns only "iPhone" if app was launched from iOS Simulator
– tesla
Sep 23 '15 at 8:46
why not-[NSString containsString]
?
– Gobe
Oct 31 '16 at 23:23
add a comment |
To include all types of "simulators"
NSString *model = [[UIDevice currentDevice] model];
if([model rangeOfString:@"Simulator" options:NSCaseInsensitiveSearch].location !=NSNotFound)
{
// we are running in a simulator
}
3
Note that this doesn't work on Xcode 7 anymore!
– radex
Sep 9 '15 at 10:55
4
It has nothing to do with Xcode 7. If you run iOS Simulator with iOS8 (from Xcode 7) then this will work. It won't work for iOS9 where [[UIDevice currentDevice] model] returns only "iPhone" if app was launched from iOS Simulator
– tesla
Sep 23 '15 at 8:46
why not-[NSString containsString]
?
– Gobe
Oct 31 '16 at 23:23
add a comment |
To include all types of "simulators"
NSString *model = [[UIDevice currentDevice] model];
if([model rangeOfString:@"Simulator" options:NSCaseInsensitiveSearch].location !=NSNotFound)
{
// we are running in a simulator
}
To include all types of "simulators"
NSString *model = [[UIDevice currentDevice] model];
if([model rangeOfString:@"Simulator" options:NSCaseInsensitiveSearch].location !=NSNotFound)
{
// we are running in a simulator
}
answered Jun 12 '15 at 18:51
jeffrjeffr
271
271
3
Note that this doesn't work on Xcode 7 anymore!
– radex
Sep 9 '15 at 10:55
4
It has nothing to do with Xcode 7. If you run iOS Simulator with iOS8 (from Xcode 7) then this will work. It won't work for iOS9 where [[UIDevice currentDevice] model] returns only "iPhone" if app was launched from iOS Simulator
– tesla
Sep 23 '15 at 8:46
why not-[NSString containsString]
?
– Gobe
Oct 31 '16 at 23:23
add a comment |
3
Note that this doesn't work on Xcode 7 anymore!
– radex
Sep 9 '15 at 10:55
4
It has nothing to do with Xcode 7. If you run iOS Simulator with iOS8 (from Xcode 7) then this will work. It won't work for iOS9 where [[UIDevice currentDevice] model] returns only "iPhone" if app was launched from iOS Simulator
– tesla
Sep 23 '15 at 8:46
why not-[NSString containsString]
?
– Gobe
Oct 31 '16 at 23:23
3
3
Note that this doesn't work on Xcode 7 anymore!
– radex
Sep 9 '15 at 10:55
Note that this doesn't work on Xcode 7 anymore!
– radex
Sep 9 '15 at 10:55
4
4
It has nothing to do with Xcode 7. If you run iOS Simulator with iOS8 (from Xcode 7) then this will work. It won't work for iOS9 where [[UIDevice currentDevice] model] returns only "iPhone" if app was launched from iOS Simulator
– tesla
Sep 23 '15 at 8:46
It has nothing to do with Xcode 7. If you run iOS Simulator with iOS8 (from Xcode 7) then this will work. It won't work for iOS9 where [[UIDevice currentDevice] model] returns only "iPhone" if app was launched from iOS Simulator
– tesla
Sep 23 '15 at 8:46
why not
-[NSString containsString]
?– Gobe
Oct 31 '16 at 23:23
why not
-[NSString containsString]
?– Gobe
Oct 31 '16 at 23:23
add a comment |
With Swift 4.2 (Xcode 10), we can do this
#if targetEnvironment(simulator)
//simulator code
#else
#warning("Not compiling for simulator")
#endif
1
Just another copy paste
– J. Doe
Nov 9 '18 at 12:03
add a comment |
With Swift 4.2 (Xcode 10), we can do this
#if targetEnvironment(simulator)
//simulator code
#else
#warning("Not compiling for simulator")
#endif
1
Just another copy paste
– J. Doe
Nov 9 '18 at 12:03
add a comment |
With Swift 4.2 (Xcode 10), we can do this
#if targetEnvironment(simulator)
//simulator code
#else
#warning("Not compiling for simulator")
#endif
With Swift 4.2 (Xcode 10), we can do this
#if targetEnvironment(simulator)
//simulator code
#else
#warning("Not compiling for simulator")
#endif
edited Oct 10 '18 at 0:30
smileBot
16.9k65554
16.9k65554
answered Jun 11 '18 at 8:33
iHSiHS
3,87922447
3,87922447
1
Just another copy paste
– J. Doe
Nov 9 '18 at 12:03
add a comment |
1
Just another copy paste
– J. Doe
Nov 9 '18 at 12:03
1
1
Just another copy paste
– J. Doe
Nov 9 '18 at 12:03
Just another copy paste
– J. Doe
Nov 9 '18 at 12:03
add a comment |
For Swift 4.2 / xCode 10
I created an extension on UIDevice, so I can easily ask for if the simulator is running.
// UIDevice+CheckSimulator.swift
import UIKit
extension UIDevice {
/// Checks if the current device that runs the app is xCode's simulator
static func isSimulator() -> Bool {
#if targetEnvironment(simulator)
return true
#else
return false
#endif
}
}
In my AppDelegate for example I use this method to decide wether registering for remote notification is necessary, which is not possible for the simulator.
// CHECK FOR REAL DEVICE / OR SIMULATOR
if UIDevice.isSimulator() == false {
// REGISTER FOR SILENT REMOTE NOTIFICATION
application.registerForRemoteNotifications()
}
add a comment |
For Swift 4.2 / xCode 10
I created an extension on UIDevice, so I can easily ask for if the simulator is running.
// UIDevice+CheckSimulator.swift
import UIKit
extension UIDevice {
/// Checks if the current device that runs the app is xCode's simulator
static func isSimulator() -> Bool {
#if targetEnvironment(simulator)
return true
#else
return false
#endif
}
}
In my AppDelegate for example I use this method to decide wether registering for remote notification is necessary, which is not possible for the simulator.
// CHECK FOR REAL DEVICE / OR SIMULATOR
if UIDevice.isSimulator() == false {
// REGISTER FOR SILENT REMOTE NOTIFICATION
application.registerForRemoteNotifications()
}
add a comment |
For Swift 4.2 / xCode 10
I created an extension on UIDevice, so I can easily ask for if the simulator is running.
// UIDevice+CheckSimulator.swift
import UIKit
extension UIDevice {
/// Checks if the current device that runs the app is xCode's simulator
static func isSimulator() -> Bool {
#if targetEnvironment(simulator)
return true
#else
return false
#endif
}
}
In my AppDelegate for example I use this method to decide wether registering for remote notification is necessary, which is not possible for the simulator.
// CHECK FOR REAL DEVICE / OR SIMULATOR
if UIDevice.isSimulator() == false {
// REGISTER FOR SILENT REMOTE NOTIFICATION
application.registerForRemoteNotifications()
}
For Swift 4.2 / xCode 10
I created an extension on UIDevice, so I can easily ask for if the simulator is running.
// UIDevice+CheckSimulator.swift
import UIKit
extension UIDevice {
/// Checks if the current device that runs the app is xCode's simulator
static func isSimulator() -> Bool {
#if targetEnvironment(simulator)
return true
#else
return false
#endif
}
}
In my AppDelegate for example I use this method to decide wether registering for remote notification is necessary, which is not possible for the simulator.
// CHECK FOR REAL DEVICE / OR SIMULATOR
if UIDevice.isSimulator() == false {
// REGISTER FOR SILENT REMOTE NOTIFICATION
application.registerForRemoteNotifications()
}
answered Nov 25 '18 at 13:29
LukeSideWalkerLukeSideWalker
4,03911624
4,03911624
add a comment |
add a comment |
My answer is based on @Daniel Magnusson answer and comments of @Nuthatch and @n.Drake. and I write it to save some time for swift users working on iOS9 and onwards.
This is what worked for me:
if UIDevice.currentDevice().name.hasSuffix("Simulator"){
//Code executing on Simulator
} else{
//Code executing on Device
}
1
The code won't work if a user addsSimulator
word in his device name
– mbelsky
May 9 '16 at 8:11
Unfortunately with XCode 8UIDevice.current.name
reports the name of the machine the Simulator is running on (typically something like "Simon's MacBook Pro" now) so the test has become unreliable. I am still looking into a clean way to fix it.
– Michael
Sep 26 '16 at 11:04
add a comment |
My answer is based on @Daniel Magnusson answer and comments of @Nuthatch and @n.Drake. and I write it to save some time for swift users working on iOS9 and onwards.
This is what worked for me:
if UIDevice.currentDevice().name.hasSuffix("Simulator"){
//Code executing on Simulator
} else{
//Code executing on Device
}
1
The code won't work if a user addsSimulator
word in his device name
– mbelsky
May 9 '16 at 8:11
Unfortunately with XCode 8UIDevice.current.name
reports the name of the machine the Simulator is running on (typically something like "Simon's MacBook Pro" now) so the test has become unreliable. I am still looking into a clean way to fix it.
– Michael
Sep 26 '16 at 11:04
add a comment |
My answer is based on @Daniel Magnusson answer and comments of @Nuthatch and @n.Drake. and I write it to save some time for swift users working on iOS9 and onwards.
This is what worked for me:
if UIDevice.currentDevice().name.hasSuffix("Simulator"){
//Code executing on Simulator
} else{
//Code executing on Device
}
My answer is based on @Daniel Magnusson answer and comments of @Nuthatch and @n.Drake. and I write it to save some time for swift users working on iOS9 and onwards.
This is what worked for me:
if UIDevice.currentDevice().name.hasSuffix("Simulator"){
//Code executing on Simulator
} else{
//Code executing on Device
}
answered Dec 23 '15 at 12:30
euthimis87euthimis87
1,0301117
1,0301117
1
The code won't work if a user addsSimulator
word in his device name
– mbelsky
May 9 '16 at 8:11
Unfortunately with XCode 8UIDevice.current.name
reports the name of the machine the Simulator is running on (typically something like "Simon's MacBook Pro" now) so the test has become unreliable. I am still looking into a clean way to fix it.
– Michael
Sep 26 '16 at 11:04
add a comment |
1
The code won't work if a user addsSimulator
word in his device name
– mbelsky
May 9 '16 at 8:11
Unfortunately with XCode 8UIDevice.current.name
reports the name of the machine the Simulator is running on (typically something like "Simon's MacBook Pro" now) so the test has become unreliable. I am still looking into a clean way to fix it.
– Michael
Sep 26 '16 at 11:04
1
1
The code won't work if a user adds
Simulator
word in his device name– mbelsky
May 9 '16 at 8:11
The code won't work if a user adds
Simulator
word in his device name– mbelsky
May 9 '16 at 8:11
Unfortunately with XCode 8
UIDevice.current.name
reports the name of the machine the Simulator is running on (typically something like "Simon's MacBook Pro" now) so the test has become unreliable. I am still looking into a clean way to fix it.– Michael
Sep 26 '16 at 11:04
Unfortunately with XCode 8
UIDevice.current.name
reports the name of the machine the Simulator is running on (typically something like "Simon's MacBook Pro" now) so the test has become unreliable. I am still looking into a clean way to fix it.– Michael
Sep 26 '16 at 11:04
add a comment |
/// Returns true if its simulator and not a device
public static var isSimulator: Bool {
#if (arch(i386) || arch(x86_64)) && os(iOS)
return true
#else
return false
#endif
}
add a comment |
/// Returns true if its simulator and not a device
public static var isSimulator: Bool {
#if (arch(i386) || arch(x86_64)) && os(iOS)
return true
#else
return false
#endif
}
add a comment |
/// Returns true if its simulator and not a device
public static var isSimulator: Bool {
#if (arch(i386) || arch(x86_64)) && os(iOS)
return true
#else
return false
#endif
}
/// Returns true if its simulator and not a device
public static var isSimulator: Bool {
#if (arch(i386) || arch(x86_64)) && os(iOS)
return true
#else
return false
#endif
}
answered Feb 28 '18 at 13:39
Pratyush PratikPratyush Pratik
481410
481410
add a comment |
add a comment |
SWIFT 4 Solution
static let isSimulator: Bool = {
return TARGET_OS_SIMULATOR == 1
}()
TARGET_OS_SIMULATOR
is located in Darwin.TargetConditionals.swift
file.
add a comment |
SWIFT 4 Solution
static let isSimulator: Bool = {
return TARGET_OS_SIMULATOR == 1
}()
TARGET_OS_SIMULATOR
is located in Darwin.TargetConditionals.swift
file.
add a comment |
SWIFT 4 Solution
static let isSimulator: Bool = {
return TARGET_OS_SIMULATOR == 1
}()
TARGET_OS_SIMULATOR
is located in Darwin.TargetConditionals.swift
file.
SWIFT 4 Solution
static let isSimulator: Bool = {
return TARGET_OS_SIMULATOR == 1
}()
TARGET_OS_SIMULATOR
is located in Darwin.TargetConditionals.swift
file.
answered Apr 11 '18 at 7:09
kamwysockamwysoc
4,28711836
4,28711836
add a comment |
add a comment |
Apple has added support for checking the app is targeted for the simulator with the following:
#if targetEnvironment(simulator)
let DEVICE_IS_SIMULATOR = true
#else
let DEVICE_IS_SIMULATOR = false
#endif
add a comment |
Apple has added support for checking the app is targeted for the simulator with the following:
#if targetEnvironment(simulator)
let DEVICE_IS_SIMULATOR = true
#else
let DEVICE_IS_SIMULATOR = false
#endif
add a comment |
Apple has added support for checking the app is targeted for the simulator with the following:
#if targetEnvironment(simulator)
let DEVICE_IS_SIMULATOR = true
#else
let DEVICE_IS_SIMULATOR = false
#endif
Apple has added support for checking the app is targeted for the simulator with the following:
#if targetEnvironment(simulator)
let DEVICE_IS_SIMULATOR = true
#else
let DEVICE_IS_SIMULATOR = false
#endif
answered Jul 11 '18 at 22:43
David CorbinDavid Corbin
4372922
4372922
add a comment |
add a comment |
if nothing worked, try this
public struct Platform {
public static var isSimulator: Bool {
return TARGET_OS_SIMULATOR != 0 // Use this line in Xcode 7 or newer
}
}
add a comment |
if nothing worked, try this
public struct Platform {
public static var isSimulator: Bool {
return TARGET_OS_SIMULATOR != 0 // Use this line in Xcode 7 or newer
}
}
add a comment |
if nothing worked, try this
public struct Platform {
public static var isSimulator: Bool {
return TARGET_OS_SIMULATOR != 0 // Use this line in Xcode 7 or newer
}
}
if nothing worked, try this
public struct Platform {
public static var isSimulator: Bool {
return TARGET_OS_SIMULATOR != 0 // Use this line in Xcode 7 or newer
}
}
answered Sep 17 '18 at 13:33
Aklesh RathaurAklesh Rathaur
1,1461216
1,1461216
add a comment |
add a comment |
In my opinion, the answer (presented above and repeated below):
NSString *model = [[UIDevice currentDevice] model];
if ([model isEqualToString:@"iPhone Simulator"]) {
//device is simulator
}
is the best answer because it is obviously executed at RUNTIME versus being a COMPILE DIRECTIVE.
11
I disagree. This code ends up in your product, whereas a compiler directive keeps the - on the device unnecessary - routine out.
– nine stones
Oct 24 '13 at 3:16
1
The compiler directives work because the device and simulators are completely different compile targets - ie you wouldn't use the same binary on both. It has to be compiled to different hardware, so it makes sense in that case.
– Brad Parks
Mar 12 '14 at 22:52
Being executed at RUNTIME makes it the worst possible answer.
– gnasher729
Apr 5 '16 at 8:43
add a comment |
In my opinion, the answer (presented above and repeated below):
NSString *model = [[UIDevice currentDevice] model];
if ([model isEqualToString:@"iPhone Simulator"]) {
//device is simulator
}
is the best answer because it is obviously executed at RUNTIME versus being a COMPILE DIRECTIVE.
11
I disagree. This code ends up in your product, whereas a compiler directive keeps the - on the device unnecessary - routine out.
– nine stones
Oct 24 '13 at 3:16
1
The compiler directives work because the device and simulators are completely different compile targets - ie you wouldn't use the same binary on both. It has to be compiled to different hardware, so it makes sense in that case.
– Brad Parks
Mar 12 '14 at 22:52
Being executed at RUNTIME makes it the worst possible answer.
– gnasher729
Apr 5 '16 at 8:43
add a comment |
In my opinion, the answer (presented above and repeated below):
NSString *model = [[UIDevice currentDevice] model];
if ([model isEqualToString:@"iPhone Simulator"]) {
//device is simulator
}
is the best answer because it is obviously executed at RUNTIME versus being a COMPILE DIRECTIVE.
In my opinion, the answer (presented above and repeated below):
NSString *model = [[UIDevice currentDevice] model];
if ([model isEqualToString:@"iPhone Simulator"]) {
//device is simulator
}
is the best answer because it is obviously executed at RUNTIME versus being a COMPILE DIRECTIVE.
edited May 21 '14 at 15:35
Eric
1,72832638
1,72832638
answered Sep 9 '13 at 14:45
user1686700user1686700
427145
427145
11
I disagree. This code ends up in your product, whereas a compiler directive keeps the - on the device unnecessary - routine out.
– nine stones
Oct 24 '13 at 3:16
1
The compiler directives work because the device and simulators are completely different compile targets - ie you wouldn't use the same binary on both. It has to be compiled to different hardware, so it makes sense in that case.
– Brad Parks
Mar 12 '14 at 22:52
Being executed at RUNTIME makes it the worst possible answer.
– gnasher729
Apr 5 '16 at 8:43
add a comment |
11
I disagree. This code ends up in your product, whereas a compiler directive keeps the - on the device unnecessary - routine out.
– nine stones
Oct 24 '13 at 3:16
1
The compiler directives work because the device and simulators are completely different compile targets - ie you wouldn't use the same binary on both. It has to be compiled to different hardware, so it makes sense in that case.
– Brad Parks
Mar 12 '14 at 22:52
Being executed at RUNTIME makes it the worst possible answer.
– gnasher729
Apr 5 '16 at 8:43
11
11
I disagree. This code ends up in your product, whereas a compiler directive keeps the - on the device unnecessary - routine out.
– nine stones
Oct 24 '13 at 3:16
I disagree. This code ends up in your product, whereas a compiler directive keeps the - on the device unnecessary - routine out.
– nine stones
Oct 24 '13 at 3:16
1
1
The compiler directives work because the device and simulators are completely different compile targets - ie you wouldn't use the same binary on both. It has to be compiled to different hardware, so it makes sense in that case.
– Brad Parks
Mar 12 '14 at 22:52
The compiler directives work because the device and simulators are completely different compile targets - ie you wouldn't use the same binary on both. It has to be compiled to different hardware, so it makes sense in that case.
– Brad Parks
Mar 12 '14 at 22:52
Being executed at RUNTIME makes it the worst possible answer.
– gnasher729
Apr 5 '16 at 8:43
Being executed at RUNTIME makes it the worst possible answer.
– gnasher729
Apr 5 '16 at 8:43
add a comment |
This worked for me best
NSString *name = [[UIDevice currentDevice] name];
if ([name isEqualToString:@"iPhone Simulator"]) {
}
2
On Xcode 7.3, iPhone 6 Plus Simulator returns"iPhone"
.
– Eric
May 11 '16 at 10:32
add a comment |
This worked for me best
NSString *name = [[UIDevice currentDevice] name];
if ([name isEqualToString:@"iPhone Simulator"]) {
}
2
On Xcode 7.3, iPhone 6 Plus Simulator returns"iPhone"
.
– Eric
May 11 '16 at 10:32
add a comment |
This worked for me best
NSString *name = [[UIDevice currentDevice] name];
if ([name isEqualToString:@"iPhone Simulator"]) {
}
This worked for me best
NSString *name = [[UIDevice currentDevice] name];
if ([name isEqualToString:@"iPhone Simulator"]) {
}
answered May 11 '16 at 6:40
ManiMani
24926
24926
2
On Xcode 7.3, iPhone 6 Plus Simulator returns"iPhone"
.
– Eric
May 11 '16 at 10:32
add a comment |
2
On Xcode 7.3, iPhone 6 Plus Simulator returns"iPhone"
.
– Eric
May 11 '16 at 10:32
2
2
On Xcode 7.3, iPhone 6 Plus Simulator returns
"iPhone"
.– Eric
May 11 '16 at 10:32
On Xcode 7.3, iPhone 6 Plus Simulator returns
"iPhone"
.– Eric
May 11 '16 at 10:32
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f458304%2fhow-can-i-programmatically-determine-if-my-app-is-running-in-the-iphone-simulato%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'm not sure a pre-processor directive is dynamic (though it might be what you were looking for anyway). The directive means that you actually knew, when you built it, where it was going to wind up running.
– WiseOldDuck
Apr 15 '16 at 19:18