Reputation: 6064
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.
Upvotes: 3
Views: 1668
Reputation: 90551
One way of doing this is to have a run-script build phase which writes a .h file that #define
s 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.
Upvotes: 1