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 ?

Blaszard
  • 30,954
  • 51
  • 153
  • 233
Hoang Pham
  • 6,899
  • 11
  • 57
  • 70

8 Answers8

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.

caramba
  • 21,963
  • 19
  • 86
  • 127
stubotnik
  • 1,952
  • 2
  • 17
  • 31
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

Andrew Bullock
  • 36,616
  • 34
  • 155
  • 231
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)

Andreas Bonini
  • 44,018
  • 30
  • 122
  • 156
  • 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 08 '10 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. – Andreas Bonini Jan 08 '10 at 15:33
  • 18
    I don't understand the downvotes. The answer is technically correct and well written. – Andreas Bonini Jan 08 '10 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. – David Thomas Jan 08 '10 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! – Mongus Pong Jan 08 '10 at 15:57
  • Can this answer be updated? I did not find this directory on iOS 8... –  Jul 30 '16 at 20:18
  • @DaMaxContent: The directory is still valid for every version of iOS, as this is part of the POSIX-API afaik. However, the `/etc/`-directory is often a symbolic link. Try navigating to `/private/etc/hosts`, if you cannot access `/etc/hosts` directly – unknown6656 Nov 05 '16 at 13:33
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.

Simon East
  • 55,742
  • 17
  • 139
  • 133
  • 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 '11 at 13:35
  • 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 '10 at 12:14
  • 2
    .. And change the default password from alpine to something else! – Tieme Nov 05 '12 at 09: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?

oefe
  • 19,298
  • 7
  • 47
  • 66
  • 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 08 '10 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.

Jaanus
  • 17,688
  • 15
  • 65
  • 110
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.

ekscrypto
  • 3,718
  • 1
  • 24
  • 38
  • 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 '14 at 03:49
  • This approach is good if you don't intend to use cookie. – DUzun Oct 09 '14 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 )' – codeslapper Jul 14 '17 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.

Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152