In this post, I will show you how we can redirect all the requests from an iOS application to a local server regardless of whether the app is a build running on Xcode or an application installed from the App Store.
Sometimes we have bugs in our app caused by an error on the server or an error related to how the mobile app treats requests. It is hard to detect the error in those cases and debug it without hitting the local server.
If you have an app used by millions of users, seeing the logs can take too much time. In my experience as a developer, I have come across bugs that are only reproducible with the App Store build and not locally, making it tough to debug and taking too much time to fix it. Those are only a few reasons why we should use Charles to proxy the requests of an app to a local environment for testing.
What is Charles?
Charles is an HTTP proxy / HTTP monitor / Reverse Proxy that enables a developer to view all of the HTTP and SSL / HTTPS traffic between their machine and the Internet. This includes requests, responses and the HTTP headers (which contain the cookies and caching information).
Prerequisites: The only thing you have to install beforehand is the Charles app.
The first step is running your server on your local machine. After that, you have to run the Charles app. Then you have to proxy the requests from your phone to your local machine. To do that, you have to follow the following steps:
1. On your phone, go to
Settings -> Wifi and click on the info icon of the Wifi you’re connected to. It should be the same network your local machine is connected to.
2. Scroll down and select
Configure Proxy, choose
manual and fill out the fields
server (the local IP of your machine) and
8888 since it’s the default one that Charles uses, you can change it later, though).
Note: To get your local IP, you can run this command if you’re using Mac OS X
ipconfig getifaddr en0.
3. Open your app on your phone. After the app makes the first request, you will see a pop-up on your Charles app asking if you want to accept requests from a device. Click yes, and that’s it!.
We have our app running on our phone and hitting our local server. I hope it is as helpful for you as it is for me.