Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode project how to detect target programmatically or how to use env vars

I want to do an Application test that parses some json, stores to core data, and reads out some objects.

How can my code know if it's being run as part of a test or normal run? Just some way to know "are we in test target"? Because the app when it fires up now kicks off a bunch of requests to populate my coredata with info from the server. I don't want it to do this during my tests. I want to fire up the App, read HARDCODED json from a file and store this using the same methods as otherwise into coredata, and verify the results.

If someone could explain how to pass specific key-value pairs on a per target basis that can be read from within the app, I would be even more delighted.

like image 775
jpswain Avatar asked Aug 06 '11 03:08

jpswain


People also ask

How do I find targets in Xcode?

Click on the project (the very first object in the tree) in the project navigator and you'll get a view with project settings on the right and targets on the left.

How do I find the target name in Swift?

And now in Swift, you may call AppTargetName() and get your target name, without need to modify Info.

What is the difference between project and target in Xcode?

A target defines a single product; it organizes the inputs into the build system—the source files and instructions for processing those source files—required to build that product. Projects can contain one or more targets, each of which produces one product.

What is target in IOS Xcode?

A Target specifies a product to build and contains the instructions for building the product from a set of files in a project or workspace. An Xcode scheme defines a collection of targets to build, a configuration to use when building, and a collection of tests to execute.


1 Answers

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.

like image 124
jpswain Avatar answered Oct 22 '22 10:10

jpswain