3

I often work on iOS projects alongside a development server, which is always running on the same machine that I'm using to build the app. A typical scenario is that I have a rails app running on http://localhost:3000 and I'm testing on the device. In order to test on the device, I need a resolved IP address so that my app can talk to my development server on the local network.

I know that I can use GCC_PREPROCESSOR_DEFINITIONS to set some environment variable into a preprocessor macro, and I know that I can get the build machine's IP address by running ifconfig | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}' in a bash script.

What I want to do is define MY_SERVER_URL in GCC_PREPROCESSOR_DEFINITIONS using a value such as '@"http://${BUILD_MACHINE_IP_ADDRESS}:3000"', but what I can't figure out is how to set ${BUILD_MACHINE_IP_ADDRESS} using the ifconfig script above.

I have considered editing a configuration file using a run script build phase, but the ramifications of a change like this are irritating. For example, I wouldn't want the file included in source control because my IP address changes frequently and it would cause conflicts. But I would need it to be included in the project file so that the application can access it. This would lead to a file that's required for the project to build but isn't included in the source control, which is ugly.

I've also tried running export BUILD_MACHINE_IP_ADDRESS=`ifconfig | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}` in a run script build phase but that doesn't yield a result.

3
  • How about this - BUILD_MACHINE_IP_ADDRESS=$(ifconfig | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}') Commented May 26, 2013 at 1:13
  • @Jaypal yes that's what I want to do, but I don't know where I would do that to make the variable available at compile time. Commented May 26, 2013 at 1:35
  • You can either export that variable on command line or add it to your profile file to make it globally available every time you open a terminal. Commented May 26, 2013 at 1:38

1 Answer 1

1

One way of doing this is to have a run-script build phase which writes a .h file that #defines the macro and is included your sources.

I don't believe that writing a .xcconfig during build will have any effect on that build.

Exporting environment variables from run-script build phases won't work for multiple reasons. First of all, environment variables may be created from build settings but don't work the other way around. Second, even if they did, a child process can't set environment variables in its parent.

For another approach entirely, have you looked into using the .local host name of your development machine instead of a numeric IP address? Check the Sharing pane of System Preferences, which will show you the domain name for your system.

Sign up to request clarification or add additional context in comments.

2 Comments

When I said configuration file in the question I was thinking more along the lines of a .json or .plist file, I should have probably been more specific. Using the .local name of the machine would work for a project which only I work on, but as soon as you introduce a team (each with their own development server) that falls down. I guess there's no ideal way to do this, I'll probably go with the .h file. Thanks :)
Yes to "using the .local host name of your development machine instead of a numeric IP address".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.