3

I'm using two targets, one for our production app and one for our staging app, and I added STAGE to both Debug and Release in Active Compilation Conditions for the Stage target in order to change API calls from our production backend to our testing backend. It all works when debugging, on both real devices and on the simulators, but when I upload the app so our internal testers can try the app in Testflight, they get the API calls from the production. What have I missed?

It seems like the flag doesn't work when I archive the Stage target. I have only added Active Compilation Conditions to the Stage target, just like the image below shows, and I'm using the following code to change the API variables:

    #if STAGE
       // setting up stage variables
       ...
    #else
       // setting up production variables
       ...
    #endif

enter image description here

EDIT: Seems like I hadn't understood how to do this. I didn't need two targets, just setting up different schemes. By doing that, I could easily add custom flags based on the scheme, instead of setting up different targets.

More reading: https://medium.com/@pablosanchezdev/managing-different-environments-and-configurations-in-xcode-for-ios-projects-6c70d46e1b22

Rickard Elimää
  • 7,107
  • 3
  • 14
  • 30
  • 1
    Hmm... This is a compiler condition, not a run-time condition, so TestFlight is not part of the issue. Confirm that you have your build settings correct, and that you are building the correct target. Add this line: `x = 1` inside your `#else` block, then Archive. If the Archive build process fails due to `error: use of unresolved identifier 'x'` then it looks like you are not archiving the correct target. – DonMag Oct 08 '19 at 13:07
  • Thanks for the advice. A smart way of checking. I added x = 1 in the `#else` block and archived, but I didn't get an error. – Rickard Elimää Oct 08 '19 at 13:14
  • OK - then the STAGE conditional **is** being processed. My next suggestion would be to put something else inside your `#if` block that would be visible when the app runs, then archive, submit and make it available for TestFlight. If that succeeds (the user can see the whatever), then you'll need to find where else the issue could be happening. Maybe a server redirect? Maybe the production/testing server URL is being changed somewhere else in the code? – DonMag Oct 08 '19 at 13:23
  • Hi again, and thanks for taking your time. I did that, and the image didn't change as well. So it's something about the flag that keeps getting ignored. Perhaps I understood things wrong, and this isn't how _Active Compilation Conditions_ should be used. I tried the older _Other Swift Flags_ but I never got that to work while debugging. – Rickard Elimää Oct 09 '19 at 07:27
  • 1
    Well, I understand it the same as you... I work in Obj-C, which uses Preprocessor Macros, but from what I'm reading (and quick testing seems to confirm) Swift's "Active Compilation Conditions" is essentially the same thing (slightly different syntax). The fact you were able to Archive without error (using that `x = 1` line) also confirms that the `#else` block is not even being compiled. My next guess would be (and it may sound dumb) --- are you sure the new "stage" build is actually being put into TestFlight, and that is what your testers are using? – DonMag Oct 09 '19 at 12:15
  • Did you ever find an answer? I'm running into the same issue. – David Jan 05 '23 at 01:31
  • 1
    @David Just like my edit says: Use schemes instead of targets. :) – Rickard Elimää Jan 05 '23 at 09:26

0 Answers0