Requests to local machine from the iPhone using Charles Web Proxy
My setup:
- iMac connected to Airport Extreme
- iPhone connected to Airport Extreme
- Server running on the iMac
- 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.
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
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.
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.
From that same screen on your iOS device (WiFi -> Network info for the network you're connected to), set the Manual proxy
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.
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
- Start your server on your macOS (like a rails server for example)
- 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)
- 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.