preface

In the initial contact with iOS development, switching between formal and test network environment is by switching network address domain name, local definition of a macro definition to achieve domain name switch, this way is really more convenient. Later, I learned another way to switch network environment, that is, to switch host, which does not need to do additional configuration in the engineering code. At the beginning, I always used the environment configured by my colleagues, but later I found that sometimes it was not very convenient, so I tried it on my own computer, and finally I successfully switched to the formal test environment on the real machine, during which I also encountered many pits. Here is how to configure it.

This section describes how to install SwitchHosts

The official website

GitHub

The installation

  • Install using terminal commands

    brew cask install switchhosts
    Copy the code
  • After successful installation

  • Just paste the host to be added to the specified host name and turn on the switch to switch (note: the host of the computer can be switched successfully. When you connect the real computer to the wifi shared by the computer, you still find that the switch is not switched, then you need to use the Dnsmasq tool).

Dnsmasq Installation and configuration

DNSmasq is a compact and convenient tool for configuring DNS and DHCP. It is suitable for small networks. It provides DNS function and optional DHCP function. It serves locally applicable domain names that don’t show up on global DNS servers. The DHCP server is integrated with the DNS server and allows the ADDRESSES assigned by the DHCP server to be resolved in the DNS server. The DHCP addresses and related commands can be configured on each host or a core device (such as a router). DNSmasq supports both static and dynamic DHCP configuration modes.

— Baidu Baike

The installation

brew link dnsmasq
Copy the code

The configuration file

resolv-file=/usr/localThe/etc/resolv. Dnsmasq. Conf strict - order listen - address = 127.0.0.1 addn - hosts = / usr /local/etc/dnsmasq.hosts
conf-dir=/usr/local/etc/dnsmasq.d
cache-size=10000
Copy the code

Noun explanation:

  • resolv-fileUpstream DNS service configuration
  • strict-orderStrictly follow the configuration sequence in the preceding files
  • listen-addressAddress to listen for requests (127.0.0.1: local only, 0.0.0.0: anyone)
  • addn-hostsSome parsing results you need
  • conf-dirOther Configuration paths
  • cache-sizeThe cache size

use

Sudo brew services stop dnsmasq Sudo brew services restart dnsmasq Refresh the DNS cache sudo killall -hup mDNSResponderCopy the code

Note: If you do not switch to the desired environment after clicking SwitchHosts, you can run the above commands in sequence

Mobile phone configuration

Delete the default DNS and add the COMPUTER’S IP address, as shown below:

Finally open the mobile phone access found that the test environment has been connected, very happy.