11

I have a client-server App, in order to test it with Simulator I have a server on a virtual machine and I change mac's hosts file (/etc/hosts) so I can get there.

It works fine in XCode 5, but on XCode 6 the server cannot be reached. When trying to open an NSURLConnection to it I get -

Error Domain=kCFErrorDomainCFNetwork Code=310 "There was a problem communicating with the secure web proxy server (HTTPS)." UserInfo=0x78b3fc50 {_kCFStreamErrorCodeKey=-2096,
NSErrorFailingURLStringKey=https://xxx.yyy.zzz/mobile/login, NSErrorFailingURLKey=https://xxx.yyy.zzz/mobile/login, NSLocalizedDescription=There was a problem communicating with the secure web proxy server (HTTPS)., _kCFStreamErrorDomainKey=4, NSLocalizedRecoverySuggestion=Please check your proxy settings. For help with this problem, contact your system administrator.

Of course the server can be reached from Safari by typing https://xxx.yyy.zzz, it answers to ping and it still works if I turn on XCode 5.

I figure that the Simulator ignores the changes for the mac's hosts file, or maybe it uses it's own private hosts file.

If I enter some "hard coded resolving" (translating in the code the xxx.yyy.zzz to it's ip address) the server can be reached.

Anyone has any idea how to solve this?

Yoshkebab
  • 770
  • 1
  • 7
  • 14
  • What does "it answers to ping and it still works if I turn on XCode 5" mean? Ping from OSX command line? If so what has Xcode 5 got to do with it? – trojanfoe Sep 22 '14 at 11:55
  • I mean that I can ping it from OSX command line, and if I launch the same app from XCode 5 simulator, I'm able to reach the server. I can't reach it only from XCode 6 simulator. – Yoshkebab Sep 23 '14 at 07:46
  • This question has some additional answers: http://stackoverflow.com/questions/26058211/ios-8-xcode-6-simulator-is-not-using-http-proxy-anymore – Ian Dunn Nov 27 '14 at 17:10

3 Answers3

17

Solution:

Make sure you add each host alias on different line:

# Wrong!
  127.0.0.1 example.com www.example.com

# Good
  127.0.0.1 example.com
  127.0.0.1 www.example.com

My story:

I had the same issue with Xcode 6.

There is a bug or behaviour change in OSX related to /etc/hosts.

If I add more host aliases on the same line in OSX's /etc/hosts file, iOS simulator gives me the same error. But if I add each host alias on it's own line, iOS simulator works as I expect it to.

DUzun
  • 1,693
  • 17
  • 15
  • 2
    This *did* work for me. Wow ... what an awful little gotcha. Seems like a bad implementation of a `hosts` file parser. – Brendan Moore Aug 15 '17 at 19:46
  • 1
    Fwif, I noticed that for example anything ending in _.dev_ won't work. Like _test.dev_ will fail while _test.local_ works (or even _test.fuck_). All praise Apple. – maryisdead Dec 08 '17 at 10:01
  • I'm using Xcode 8, and I still have the same issue. This solution worked!! Thank you so much!! – kanji Mar 07 '18 at 13:05
4

Have you tried flushing the DNS cache with the command

sudo dscacheutil -flushcache

before quitting and relaunching the iOS Simulator ?

Cyrille
  • 25,014
  • 12
  • 67
  • 90
0

IPv6

If all of: your application, your mac, your network, DNS and your backend support IPv6, you need to edit the hosts file with an IPv6 entry.

2001:0db8:85a3:0000:0000:8a2e:0370:7334 example.com

It seems that IPv6 resolves first so any IPv4 entry in the hosts file will be ignored if there is a working IPv6 infrastructure.

Thibault D.
  • 10,041
  • 3
  • 25
  • 56