Search code examples
iosswiftbuildipa

Swift: different builds connect to different servers


I have a Swift iOS project that connects to a server using HTTP. There are two servers, one for development and one for production. When I edit and test my app in Xcode, I want it to connect to the development server. Then when the code is ready, I use the following commands to build the IPA for release:

xcodebuild archive -scheme MyApp -archivePath ~/Archive/MyApp
xcodebuild -exportArchive -archivePath ~/Archive/MyApp.xcarchive -exportPath ~/IPA/MyApp -exportFormat ipa -exportProvisioningProfile "My Provisioning Profile"

and I want this release IPA to connect to the production server. So what's the best way to do this?


Solution

  • Step 1: creating a Swift Compiler Custom Flag

    1. In Xcode open the Project Navigator J
    2. Select the project root
    3. Select your target
    4. Look for the Swift Compiler - Custom Flags > Other Swift Flags section
    5. Add to Debug this entry: -D DEBUG

    enter image description here

    Step 2: create a Swift Preprocessor Macro

    Then write this in your code

    #if DEBUG
    let server = "http://www.yourserver.com/debug/"
    #else
    let server = "http://www.yourserver.com/production/"
    #endif