112

I am developing an application that query to the server. In my Mac, I use the hosts file to change the dns to point to a local server within my local area network.

Now I need to test it with my iPhone, the problem is that my iPhone does not recognize that server due to the missing configuration for dns.

On my Mac or Windows, I simply add: 192.168.0.20 http://www.google.com to the hosts file.

Now how do I let my iPhone know the URL: http://www.google.com ?

7
  • Jailbreak your iPhone, download iFile, navigate to /etc/ and open the file called hosts in the text viewer. Now you can edit it.
    – user1032867
    Nov 6, 2011 at 22:41
  • Local Development on iPhone with OSX and Charles Web Proxy deployfx.com/2011/08/…
    – jfroom
    Feb 15, 2012 at 4:29
  • 1
    OSX iOS Simulator and update your /etc/hosts file.
    – dmarvasti
    Aug 20, 2013 at 21:25
  • Alternatively: install Bonjour (sudo apt-get install avahi-daemon avahi-utils) and access via hostname.local on iphone/ipad Sep 25, 2015 at 2:06
  • I was surprised not to see an answer like this one, which worked for me: stackoverflow.com/a/41857012/470749
    – Ryan
    Jan 25, 2017 at 16:44

8 Answers 8

84

Another option here is to have your iPhone connect via a proxy. Here's an example of how to do it with Fiddler (it's very easy):

http://conceptdev.blogspot.com/2009/01/monitoring-iphone-web-traffic-with.html

In that case any dns lookups your iPhone does will use the hosts file of the machine Fiddler is running on. Note, though, that you must use a name that will be resolved via DNS. example.local, for instance, will not work. example.xyz or example.dev will.

12
  • 3
    This is the simplest solution. I should have thought of this!!
    – jimplode
    Jul 7, 2011 at 9:18
  • 1
    Awesome. This worked a treat for me!
    – Sniffer
    Dec 7, 2011 at 13:58
  • 1
    Same thing works great in osx, setup a HTTP proxy in network settings i.imgur.com/h5zEm.png
    – antonj
    Jan 29, 2012 at 12:09
  • 2
    Local Development on iPhone with OSX and Charles Web Proxy deployfx.com/2011/08/…
    – jfroom
    Feb 15, 2012 at 4:30
  • 1
    This is a great tool to have in the arsenal for any mobile app development - not just for DNS lookups Jun 11, 2012 at 17:30
38

This doesn't directly answer your question, but it does solve your problem...

What make of router do you have? Your router firmware may allow you to set DNS records for your local network. This is what I do with the Tomato firmware

3
  • thanks, this is what I did to solve the issue ;)
    – Hoang Pham
    Jan 22, 2010 at 16:43
  • I used this approach too, except I'm using pfsense.
    – codewise
    Apr 9, 2013 at 21:39
  • how will it work, i too have same issue, i am on cisco wrt router
    – Amitg2k12
    May 1, 2014 at 11:52
31

Not programming related, but I'll answer anyway. It's in /etc/hosts.

You can change it with a simple text editor such as nano.

(Obviously you would need a jailbroken iphone for this)

7
  • 3
    You can't use nano on a vanilla iphone to directly get to /etc/hosts. I would assume is about an app that would be distributed, in which case you can't mess with files outside your sandbox.
    – Jaanus
    Jan 8, 2010 at 15:32
  • @Jaanus: of course I meant with a jailbroken iphone. And he never said anything about an app being redistributed; he simply mentioned he needed to test it on his iphone which had a dns problem. Jan 8, 2010 at 15:33
  • 18
    I don't understand the downvotes. The answer is technically correct and well written. Jan 8, 2010 at 15:37
  • 2
    I'll restore some karma for the edit; if the down-votes were used because of the implicit 'for distributed app' then while I can understand them, I think the downvoters should've left a comment to explain that. Jan 8, 2010 at 15:47
  • 2
    I presume he only needs to change the hosts file so he can test his app. Once tested, it shouldnt need the hosts file changed. Perfectly acceptable to recommend jailbreaking here! Jan 8, 2010 at 15:57
14

I just edited my iPhone's 'hosts' file successfully (on Jailbroken iOS 4.0).

  • Installed OpenSSH onto iPhone via Cydia
  • Using a SFTP client like FileZilla on my computer, I connected to my iPhone
    • Address: [use your phone's IP address or hostname, eg. simophone.local]
    • Username: root
    • Password: alpine
  • Located the /etc/hosts file
  • Made a backup on my computer (in case I want to revert my changes later)
  • Edited the hosts file in a decent text editor (such as Notepad++). See here for an explanation of the hosts file.
  • Uploaded the changes, overwriting the hosts file on the iPhone

The phone does cache some webpages and DNS queries, so a reboot or clearing the cache may help. Hope that helps someone.

Simon.

3
  • 1
    @Simon I followed the same steps, but it didn't work. Then i tried to change encoding settings, and got it! You have to convert your host file to ANSI in Notepad++ before you save it. For whom failed to get the host file working; click "Convert to ANSI" under "Encoding" menu.
    – trkaplan
    Sep 26, 2010 at 12:14
  • 2
    You can also use the iFile app from Cydia to edit the hosts file on your device without having to use a computer. Ad@m
    – kirb
    Jun 17, 2011 at 13:35
  • 2
    .. And change the default password from alpine to something else!
    – Tieme
    Nov 5, 2012 at 9:05
7

It might exist, but you cannot change it on a non-jailbreaked iPhone.

Assuming that your development webserver is on a Mac, why don't you simply use its Bonjour name (e.g. MyMac.local.) instead of myrealwebserverontheinternet.com?

1
  • all the web services exist in a server within my LAN network. I have a domain name and I would like to map my domain name to this server as well. and immagine that someone gives you an address for all of his webservices, he ask you to query these services using this address, he can show you how to add hosts file to mac, windows, but not iphone :-s
    – Hoang Pham
    Jan 8, 2010 at 15:54
6

Don't change the DNS on the phone. Instead, connect with wifi to the local network and you are all set.

At my office, we have internal servers with internal DNS that are not exposed to the Internet. I just connect with iPhone to the office wifi and can then access them fine.

YMMV, but instead of configuring the phone DNS, it feels to me that just setting up local internal DNS and wifi is a cleaner and easier solution.

6

In case anybody else falls onto this page, you can also solve this by using the Ip address in the URL request instead of the domain:

NSURL *myURL = [NSURL URLWithString:@"http://10.0.0.2/mypage.php"];

Then you specify the Host manually:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL];
[request setAllHTTPHeaderFields:[NSDictionary dictionaryWithObjectAndKeys:@"myserver",@"Host"]];

As far as the server is concerned, it will behave the exact same way as if you had used http://myserver/mypage.php, except that the iPhone will not have to do a DNS lookup.

100% Public API.

3
  • 2
    However this will only work for the first request, if the mypage.php does any other request to myserver then the app won't do the conversion.
    – htafoya
    Apr 12, 2014 at 3:49
  • This approach is good if you don't intend to use cookie.
    – DUzun
    Oct 9, 2014 at 15:00
  • 1
    Nah. This didn't work either. We are using SSL Pinning, so domain mismatch messed it up: '017-07-13 10:21:42.244723-0400 ApperName[1015:449748] libMobileGestalt MobileGestaltSupport.m:153: pid 1015 (ApperName) does not have sandbox access for frZQaeyWLUvLjeuEK43hmg and IS NOT appropriately entitled 2017-07-13 10:21:42.244886-0400 UnionBank[1015:449748] libMobileGestalt MobileGestalt.c:550: no access to InverseDeviceID (see <rdar://problem/11744455>)' Jul 14, 2017 at 14:22
1

No, an iPhone application can only change stuff within its own little sandbox. (And even there there are things that you can't change on the fly.)

Your best bet is probably to use the servers IP address rather than hostname. Slightly harder, but not that hard if you just need to resolve a single address, would be to put a DNS server on your Mac and configure your iPhone to use that.

Not the answer you're looking for? Browse other questions tagged or ask your own question.