Programmatically determine current target (run or test) in iOS project
You should define proper value for "Preprocessor Macros" in Target Settings.
At the run time you can check it with ifdef
sentence.
How to let the app know if it's running Unit tests in a pure Swift project?
Instead of checking if the tests are running to avoid side-effects, you could run the tests without the host app itself. Go to Project Settings -> select the test target -> General -> Testing -> Host Application -> select 'None'.
Just remember to include all files you need to run the tests, as well as libraries normally included by the Host app target.
Xcode project how to detect target programmatically or how to use env vars
Never mind... figured out that it is in "Schemes" that you set this.
For example if you want TARGET=TEST to be available during Test and TARGET=RUN to show during run, just set that in your Scheme > Environment Variables > Name/Value.
Then from your app you can do:
[[[NSProcessInfo processInfo] environment] objectForKey:@"TARGET"]
Using build settings with preprocessor macros DID NOT work for me b/c my test target (for application/integration testing) is dependent on my main (not test) target, so the main target is built first and that's what runs, and you end up with main target preprocessor macros even though you are after the ones defined in the target you ran. If I missed something here someone feel free to explain please.
How can I programmatically determine if my app is running in the iphone simulator?
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.
How to dynamically change target for unit tests in Xcode 7?
If you run the tests from the command line, or from an CI tool, like Jenkins, you can instruct xcodebuild to use the build settings that you provide. A simple usage example would be:
xcodebuild -scheme SomeScheme test TEST_HOST=target
You can control almost (if not any) build setting from Xcode, like code coverage generation, build directory, derived data directory, code sign identity, etc.
How to detect if code is running in Main App or App Extension Target?
You can use a preprocessor macro:
In the project settings use the dropdown in the topbar to select your extension target:
Then:
- Click
Build Settings
- Find (or search)
Preprocessor Macros
underApple LLVM 6.0 - Preprocessing
- Add
TARGET_IS_EXTENSION
or any other name of your choice in both the debug and release sections.
Then in your code:
#ifndef TARGET_IS_EXTENSION // if it's not defined
// Do your calls to UIApplication
#endif
Related Topics
Add a Navigation Bar to a View Without a Navigation Controller
App Crashes in Background While Changing Permission - Swift
How to Code the Launchscreen Programmatically
How to Unit Test an App Extension on Xcode 6
Uisegmentedcontrol iOS 13 Clear Color
Programmatically Creating Uinavigationcontroller in iOS
How to Use Phcachingimagemanager
Xcode 5 - "No Application Records Were Found" When Trying to Validate an Archive
iPhone Only App Rejected for Not Running on iPad
How to Detect the Scroll Direction from the Uicollectionview
Error Using Swift - Instance Member Cannot Be Used on Type 'Viewcontroller'
Swiftui Foreach Index Out of Range Error When Removing Row
Using Two Versions of a Cocoapod Dependency
Canopenurl Not Working in iOS 10
How to Record Actual Sound on the Simulator Using Mic