Last Updated: August 22, 2016
·
7.785K
· artchang

Requests to local machine from the iPhone using Charles Web Proxy

My setup:

  1. iMac connected to Airport Extreme
  2. iPhone connected to Airport Extreme
  3. Server running on the iMac
  4. iPhone app wants to make requests to iMac's server

My solution:

Using any web proxy app for macOS, you can proxy all requests from your iOS device.

  1. Change your hosts file to point some domain to your localhost domain
    $ sudo vim /etc/hosts
    In the hosts file, choose a new name, localdev.com is an example
    127.0.0.1 localdev.com

  2. Get a web proxy. I use Charles - it costs money, but worth it if you do a lot of debugging of requests as well as proxying like this.

  3. All I needed to do is set the Access Control Settings to only allow a specific IP range to use the proxy. In this case, my local network on my router was in the 10.0.1.0/24 range. You can find this by going to your iOS device's Wi-Fi settings, then tap info for the network you're connected to, and look at your IP Address. Change that last digit to 0 and add the /24 for the whole range of devices that might use the proxy.

  4. From that same screen on your iOS device (WiFi -> Network info for the network you're connected to), set the Manual proxy

  5. The Server will be the IP address of your macOS device running Charles (or other web proxy). To find the IP Address, go to your Network settings on the macOS, and look for your IP Address on the ethernet/wifi connection you have.

  6. The Port will be the one Charles is listening on. It's default to 8888, but you can find it in the Charles app by tapping the cog and going to preferences.

Try it

  1. Start your server on your macOS (like a rails server for example)
  2. On your iOS device, goto the Safari browser, and type in http://localdev.com:3000 (or whatever port your server is listening, not the proxy port or anything like that)
  3. It should load!

Conclusion

What you're doing is sending all your requests through the macOS using the proxy, and since the hosts file is pointing the domain localdev.com to your localhost IP, it's just hitting the server on your mac.