Home > Software engineering >  How can I pass developer machine hostname to app
How can I pass developer machine hostname to app

Time:08-15

After much trial and error, I have failed to come up with a solution for this problem. Hoping someone might know a way.

I want to pass my development machine hostname to the app, without hardcoding it.

My idea was to make a build phase script that captures the hostname and export it

export DEV_MACHINE_HOST=hostname

and then set it as a launch argument on the RUN scheme

-dev-machine $(DEV_MACHINE_HOST)

But to no avail.

Anyone know if this can be done?

Context: I'm making a dev tool with a local server. I want to send http requests to this tool as long as I'm on the same network, so that it will work on device as well as the simulator.

CodePudding user response:

See Providing custom variables for Info.plist for an example of this. Create a Script Phase that uses PlistBuddy with something like:

/usr/libexec/PlistBuddy \
    -c "Add :DevMachineHost string `hostname`" \
       "${BUILT_PRODUCTS_DIR}/${EXECUTABLE_FOLDER_PATH}/Info.plist"

And then fetch it with Bundle.main.object(forInfoDictionaryKey:).

Alternately, you can write a script that generates a Swift file that defines a global for you. (But it's more typical to do this with Info.plist.)

A scheme can't do this, because that's just how Xcode launches the app in the debugger. It's not something that's stored with the app or that influences how the app is normally launched.

  • Related