简体   繁体   中英

Is it possible to disable the network in iOS Simulator?

I am trying to debug some inconsistent behaviour I am seeing in an application that gets its primary data from the Inte.net. I don't see the issues in the simulator, just on the device, so I'd like to reproduce the.network and connectivity environment in the simulator.

Is there a way of disabling the.network in the simulator?

(I am connecting to the Mac remotely to code, and there isn't any other choice right now, so disabling the OS.network isn't an option).

Use a simple Faraday cage to block or limit the external RF signal level.

You can make your own with aluminum foil. The openings should be smaller than the wavelength of your data service if that's what you want to block.

  • 800 MHz has a 37 cm (14") wavelength
  • 1900 MHz has a 16 cm (6") wavelength.

This works better with an actual device than with the simulator since the Mac is hard to work on when inside the Faraday cage ;-)

在此处输入图片说明

Yes. In Xcode, you can go to menu Open Developer ToolsMore Developer Tools and download " Additional Tools for Xcode ", which will have the Network Link Conditioner.

Using this tool, you can simulate different network scenarios (such as 100% loss, 3G, high-latency DNS, and more) and you can create your own custom ones as well.

I'm afraid not—the simulator shares whatever network connection the OS is using. I filed a Radar bug report about simulating network conditions a while back; you might consider doing the same.

The only way to disable the network on the iOS simulator I know is using tools like Little Snitch or Hands Off . With them you can deny/block any outgoing and ingoing network connections.

You can set it up so that it only blocks connections from the simulator app. It works like a firewall.

只需在 Mac OS X 中关闭您的 Wi-Fi。这很管用!

Download Additional tools package (Network Link Conditioner)

Description

Example in Sierra: 在此处输入图片说明

在此处输入图片说明

Since Xcode 4 (?) there is a preferences pane in /Applications/Utilities called Network Link Conditioner . Either you use one of the existing profiles or you create your own custom profile with 0 kbit/s up/downlink and 100% dropped.

Since Xcode does not provide such a feature, you will definitely go for some third-party application/ tool. Turning off the Mac network will also help to turn off the iOS Simulator network.

You can turn off you Mac Internet connection from System Preferences...Network and turn off the desired network source.

To turn off you Mac Ethernet Internet source: 以太网网络源

To turn off you Mac Wi-Fi Internet source (if your Mac is on a Wi-Fi Internet connection): 在此处输入图片说明

You can throttle the Internet connection with a third-party app such as Charles .

Hit Command + Shift + T on a Mac to set up the throttling.

One probably crazy idea or patch:

Just toggle the flag of network reachability

This is code which I use to toggle my flag at runtime by triggering the 'Simulator Memory Warning' and it's completely safe . Just make sure code should be in Debug mode only

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
#ifdef DEBUG
    isInternetAvailable = !isInternetAvailable;
#endif
}

If your app is connecting to a specific domain, you can simply add it to your /etc/hosts file and route it to a non-existing IP address in your local network... For the application it will be the same as if there wasn't any Internet connection or the server was not reachable.

sudo nano /etc/hosts

Add the following line:

192.168.1.123   example.com

Or use 127.0.0.1 if you are not running a web server on your local machine.

You can use Little Snitch to cut off network traffic to any individual process, including ones that run on the iOS simulator. That way you can keep your Internet connection and disconnect your running app.

I would suggest you using the Charles Proxy app on Mac.

It allows you using the Bandwidth Throttle feature that was created just for adjusting the network connection.

Start/Stop Throttling ⌘ command + T
Throttle Settings... ⌘ command + T + ⇧ shift

*If you create you own Preset via Add Preset with Bandwidth 0 and 0 for Download and upload, you can simulate no Internet connection. Also it is very useful to enable it only for some specific hosts.

As an alternative, you can disable your connection on Mac because all traffic from Simulator go through your computer.

With Xcode 8.3 and iOS 10.3:

XCUIDevice.shared().siriService.activate(voiceRecognitionText: "Turn off Wi-Fi")
XCUIDevice.shared().press(XCUIDeviceButton.home)

Be sure to include @available(iOS 10.3, *) at the top of your test suite file.

You could alternatively "Turn on Airplane Mode" if you prefer.

Once Siri turns off Wi-Fi or turns on Airplane Mode , you will need to dismiss the Siri dialogue that says that Siri requires internet. This is accomplished by pressing the home button, which dismisses the dialogue and returns to your app.

There are two ways to disable iOS Simulator's Internet connection:

  • Unplug your network connection
  • Turn Wi-Fi off

It's the simplest way.

您可以使用OHHTTPStubs并将网络请求存根到特定 URL 以失败。

A simple solution is to create an Airplane Mode for your Mac. Here is how to do this:

  • go into Network in System Preferences
  • click on Location dropdown menu
  • click on plus icon to add a new location
  • name the new location 'Airplane Mode' or similar, and click 'Done'
  • select the location you just created from the Location dropdown menu
  • click on each available network interface in the list at the left of the window in turn, disabling each one
  • click on the Configure iPv4 menu, and choose Off
  • for Wi-Fi, just click on the Turn Wi-Fi Off button
  • click Apply, and this location will block all network activity

When you want to turn networking back on, just select Automatic from the Location dropdown menu, and click Apply

If you have at least two Wi-Fi networks to connect is a very simple way is to use a bug in iOS simulator:

  1. quit from the simulator ( Cmd + Q ) if it is open
  2. connect your Mac to one Wi-Fi access point (it may be not connected to the Internet, but it doesn't matter)
  3. launch the simulator (menu: Xcode → Open Developer TooliOS Simulator ) and wait while it is loaded
  4. switch Wi-Fi network to the other one
  5. profit

The bug is that the simulator tries to use a network (IP?) which is not connected already.

Until you relaunched the simulator - it will have no Internet connection (even if that first Wi-Fi network you connected had an Internet connection), so you can run ( Cmd + R ) and stop ( Cmd + . ) project(s) to use the simulator without a connection, but your Mac will be connected.

Then, if you'll need to run the simulator connected - just quit and launch it.

You can use the network link conditioner on your Mac. You can download it from the Apple developer website. It should be available where we get the older versions of Xcode and iOS. With this network conditioner you can change the strength of the network from Wi-Fi to no network.

Also, when you install the network conditioner, it gets installed in the system preferences.

If you need to spoil your.network connection is different ways take a look at these utils:

ipfw or pfctl (FreeBSD/OSX) - low level packet filter. Sample of denying access to a port from localhost To disable the.network you can execute in terminal:

pfctl -e
(echo "block all"; pfctl -sr) | pfctl -f -

To stop filtering:

pfctl -d

There once was Throttle (Mac) for simulation 3G.network through shared wireless.network.

您可以改为禁用主机的网络!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM