I'm using the camera and want to check if I'm using the iOS Simulator or an actual Device, so I put this statement in my code:
#if IOS_SIMULATOR
print("It's an iOS Simulator")
#else
print("It's a device")
#endif
However when I'm running the device in the iOS Simulator it actually prints out "It's a device".
Which is the opposite. Is there some other Xcode setting or flag I can check for this?
It seems as though I would have something selected saying "Even if it's the Simulator always run as if it's a device" you know?
I'm not sure where you're getting IOS_SIMULATOR from, but it's certainly not from Apple. I suggest you use Apple's APIs for doing this. Eg:
#include <TargetConditionals.h>
#if TARGET_OS_SIMULATOR
...
#else
...
#endif
Related
I'm working on porting an iOS application to Catalyst. The Catalyst (Mac) version will have its own target.
Is there an official way to conditionally compile code just for Catalyst? Otherwise, I can add a target-specific define, but it would be better to use something more general.
As seen in the documentation Creating a Mac Version of Your iPad App, you do:
Swift:
#if targetEnvironment(macCatalyst)
// Code specific to Mac.
#else
// Code to exclude from Mac.
#endif
Objective-C:
#if TARGET_OS_MACCATALYST
// Code specific to Mac.
#else
// Code to exclude from Mac.
#endif
It is also possible to run code that is excluded from macCatalyst without having to use the #else. Note the use of ! (not).
#if !targetEnvironment(macCatalyst)
print("This code will not run on macCatalyst")
#endif
This question already has answers here:
How to detect if app is being built for device or simulator in Swift
(21 answers)
Closed 5 years ago.
Although this question has been asked many times in older environments, when I try to run #mbelsky's answer on a device running iOS 11.2, the simulator block of the code below runs, rather than the device block. When I run the code on the simulator, the simulator runs as expected.
#IBOutlet weak var testSimulatorFlag: UILabel! {
didSet {
#if IOS_SIMULATOR
testSimulatorFlag.text = "Compiler thinks this is a simulator"
#else
testSimulatorFlag.text = "Compiler thinks this is a device"
#endif
}
}
This is a screenshot of how the project's Swift-Compiler, Custom Flags is set:
Here is a sample project that demonstrates the issue. Thanks in advance for any suggestions.
I have found the most reliable way is:
#if targetEnvironment(simulator)
testSimulatorFlag.text = "Compiler thinks this is a simulator"
#else
testSimulatorFlag.text = "Compiler thinks this is a device"
#endif
My application is frequently receiving memory warnings and crashes with I enable NSLog statements and run it via XCode 6.1. It works fine in release mode by disabling NSLog statements. I am not able to memory profile the app using instruments as even instrument hangs when I run with NSLog statements enabled.
Has anyone faced this issue and know of a workaround?
This is how I am defining a macro to enable logs when running in DEBUG mode.
#ifdef DEBUG
#define MYLOG(...) NSLog(__VA_ARGS__)
....
MYLOG(#"Log something");
I am using the following and It works fine for me.
#ifdef DEBUG
#define MYLOG(format, ...) NSLog(format, ##__VA_ARGS__)
#else
#define MYLOG(...)
#endif
Found the issue. It was not NSLog. Application was crashing in Debug mode only and it was because NSZombie was enabled. Once I disabled NSZombie, it worked!
I am trying to get the MIDI over BLE working as discussed at session 501 at the WWDC 2014.
I get an error "file not found" when trying to #import CoreAudioKit/CoreAudioKit.h framework
I have included the framework in the build menu and tried putting the #import line in the .h or .m ViewController file.
It recognizes and compiles CoreBluetooth/CoreBluetooth.h with no problems.
This is a brand new installation of Xcode 6 downloaded yesterday (13 Sept 2014)
I feel like Apple has not turned on the switch for CoreAudioKit for iOS.
Any guidance on what I am doing wrong would be most gratefully received.
Thank you,
Ken
CoreAudioKit is not compatible with the iOS Simulator. Run it on a device, and it should build just fine
I you want to use the simulator for some other tests (not related to midi over bluetooth) you can use a conditional import of the framework:
#ifdef __APPLE__
#include "TargetConditionals.h"
#if TARGET_IPHONE_SIMULATOR
#compatibility_alias CABTMIDICentralViewController UIViewController;
#elif TARGET_OS_IPHONE
#import <CoreAudioKit/CoreAudioKit.h>
#endif
#endif
You still have to do optional linking of the framework in your build settings since CireAudioKit is not available at all on the simulator (OS X) platform.
I am trying to build an iOS Application for 64 bit. How do I know weather it is build for 32 bit or 64 bit ?
Does Mach-O-View can help ? if Yes, where to look for it ?
Also is there any command line for it. ?
I reffered this but it does not help me to know weather this it is built or not for 64 bit.
https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaTouch64BitGuide/ConvertingYourAppto64-Bit/ConvertingYourAppto64-Bit.html#//apple_ref/doc/uid/TP40013501-CH3-SW1
Try this:
#ifdef __LP64__
// NSLog(#"64");
#else
// NSLog(#"32");
#endif
It works for OS X, but I didn't test it for iOS (I don't have an iPhone5S :( ).
Edit:
By the way, it works fine on iPhone Simulator.