7

I'm attempting to inspect http (non-SSL) traffic using XCode 6.1 and iOS Simulator 8.1 using Charles and my localhost apache server.

I've got Charles working correctly, but it only captures traffic when I use my local network IP address: 192.168.1.X as the target host for requests in iOS.

I've tried the other suggestions from the Charles article here, but none work except for the local network IP address.

"Why not just use the local network IP?", you ask?. Well, I'd like to avoid YASCE (yet another source control exception). You see, my source code has this in networking section:

#if DEBUG
    var API_HOST = "http://localhost"
#else
    var API_HOST = "https://website.com"
#endif

I'd like to avoid forcing every developer on the team to constantly make special considerations to avoid checking in their own personal IP address every time they are committing to source control.

Is there another way that I can convince the iOS simulator to pass http://localhost through Charles, or is there a better way to handle environment-specific settings with a development team?

Albert Bori
  • 9,832
  • 10
  • 51
  • 78

4 Answers4

3

Use localhost.charlesproxy.com instead of localhost. That's setup on the charlesproxy.com DNS to point to 127.0.0.1, and always will. And because it's not literally localhost it should bypass the OS's hardwired logic for localhost.

It is also possible to use local.charles, but only if Charles is actually running and you're using it as your proxy. So I prefer the localhost.charlesproxy.com solution.

I'll update that FAQ too.

Karl von Randow
  • 474
  • 3
  • 13
  • Note: Be sure to add `localhost.charlesproxy.com` to your "Include" filter, if you have that turned on in "Recording Settings". Otherwise the requests won't show up. >.. – Albert Bori Jan 25 '16 at 21:45
  • I'm getting an error at localhost:3000 directly or via either of those addresses (local.charles:3000, localhost.charlesproxy.com:3000), when charles is running. Fresh install of charles, OSX 10.11.4. I tried using the SOCKS proxy instead and same error. Screenshot of the error for all 3 addresses: https://s3.amazonaws.com/f.cl.ly/items/0b162j1v2A2O3O1X2C3o/Charles_Error_Report.png – Jay May 10 '16 at 19:45
2

I found an acceptable work-around. It involves editing your hosts file to create an alias for localhost. Each developer will have to do this on his/her dev machine, but it should be smooth sailing after that.

Run echo '127.0.0.1 local.website.com' >> "/etc/hosts" and then change your host config code to something like this:

#if DEBUG
    var API_HOST = "http://local.website.com"
#else
    var API_HOST = "https://website.com"
#endif
Albert Bori
  • 9,832
  • 10
  • 51
  • 78
1

I am using my hostname instead of localhost. To get your hostname simply type hostname in the terminal and then change the URL to "http://your-host-name"

Evgenii
  • 36,389
  • 27
  • 134
  • 170
  • This is not an acceptable solution for this question. It does work, but it does not prevent developers from having to special-case their endpoint addresses in code. – Albert Bori Jul 08 '15 at 02:08
0

Same problem happened to me. Using my computer's name instead of "localhost" solved my problem, and enable to display it on Charles. For example, my computer's name is "sukwon" and I solved it by using "http://sukwon.local" instead of using "http://localhost"

Sukwon
  • 239
  • 2
  • 8
  • Unfortunately, it does not solve the underlying problem. If you are working on a team with two people, it's not likely that they'll have the same computer name. Then you'll constantly have conflicts in your xcodeproj file when using a shared code repo. – Albert Bori Apr 03 '15 at 15:30