How to get current computer's IP address in Xcode

后端 未结 3 1864
不思量自难忘°
不思量自难忘° 2021-02-08 17:57

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 d

3条回答
  •  萌比男神i
    2021-02-08 17:58

    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'
    

提交回复
热议问题