mverderese
mverderese

Reputation: 5334

How to get current computer's IP address in Xcode

When testing iPhone apps that connect to my local machine's server, I need to enter my computer's local IP address as opposed to localhost in order to test with devices other than the simulator. This gets annoying with dynamic IP addresses and when multiple developers are testing. Is there a code snippet that can get the IP address of the computer that is compiling code, and NOT the IP the address of the device that the application is running on (preferably in C or Objective-C, and not Swift)?

Upvotes: 13

Views: 5688

Answers (4)

Samuel Wyndham
Samuel Wyndham

Reputation: 61

For Xcode 15:

  1. Add a "Run Script" in the "Build Phases" tab of your Xcode project.
  2. Ensure your script is at the end of the build phases.
  3. Replace script with:
IP_ADDRESS=$(ipconfig getifaddr en0)
defaults write "${BUILT_PRODUCTS_DIR}/${INFOPLIST_PATH}" CompanyNameDevServerIP "$IP_ADDRESS"
  1. Access in Swift:
Bundle.main.object(forInfoDictionaryKey: "CompanyNameDevServerIP") as? String

Explanation: plistbuddy doesn't work with the new build system. Default is a drop-in replacement. The ${BUILD_PRODUCTS_DIR} is where the plist that gets chucked into your app is. The script will run on the predefined value. This is obviously super powerful and you can extend this to other things like automatic versioning or including git commits.

Upvotes: 3

Jed Lau
Jed Lau

Reputation: 326

I got this working by having a run script set the computer's IP address in the app's plist, then reading the plist value in code.

1) In your Info.plist file, add a key/value pair that will contain your computer's IP address. For this example, we'll add a key of "CompanyNameDevServerIP", of type "String". Note that this key/value pair should be prefixed uniquely, so that it doesn't conflict with Apple's keys (see here for more info).

2) In the "Build Phases" tab, add a run script that contains the following:

if [ "$CONFIGURATION" == "Debug" ]; then
  echo -n ${TARGET_BUILD_DIR}/${INFOPLIST_PATH} | xargs -0 /usr/libexec/PlistBuddy -c "Set :CompanyNameDevServerIP `ipconfig getifaddr en0`"
else
  echo -n ${TARGET_BUILD_DIR}/${INFOPLIST_PATH} | xargs -0 /usr/libexec/PlistBuddy -c "Delete :CompanyNameDevServerIP"
fi

This sets the computer's IP address in the plist that gets bundled with the build, but only in debug builds. (It's removed from the plist file in release builds.)

  • Hat tip to this blog post for providing this technique.
  • You may need to use a different interface other than en0 (see here for more info).

3) In code, retrieve this plist value to get your computer's IP address:

NSString *serverIP = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CompanyNameDevServerIP"];

Upvotes: 9

Chris Morse
Chris Morse

Reputation: 334

1) Add a "Run Script" in the "Build Phases" tab of your Xcode project that contains this:

export SERVER_IP=`ipconfig getifaddr en0`

Note: change "en0" to whichever interface matches your machine. en0 is the wifi on my machine and my hard-wire is en3. Do an "ifconfig -a" in Terminal to get the list of all of your adapters and see which is which for your machine

2) Go to your project file. Click the Project itself in the left menu then Build Settings in the right side. Go to "Apple LLVM 6.0 - Custom Compiler Flags". Under "Other C Flags" -> "Debug" define a new value called -DSERVER_IP=${SERVER_IP}

This will map your build script's results into a #DEFINE in your project

3) In your code use SERVER_IP just like you would any other #DEFINE and it will always have the value of the computer that built the code.

Upvotes: 11

picciano
picciano

Reputation: 22701

If you need this at compile time, you can just add a "Run Script" in the "Build Phases" tab of your Xcode project. Putting this into the source code will naturally return the IP address of where the code is running, not where it was built.

This script will return the primary IP address. You can modify the script to edit a Plist or whatever you need from there. PlistBuddy works well for modifying plist files at build time.

ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'

Upvotes: 2

Related Questions