Is it possible to disable the network in iOS Simulator?

IosIphoneDebuggingNetworkingIos Simulator

Ios Problem Overview


I am trying to debug some inconsistent behaviour I am seeing in an application that gets its primary data from the Internet. 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).

Ios Solutions


Solution 1 - Ios

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 ;-)

Enter image description here

Solution 2 - Ios

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.

Solution 3 - Ios

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.

Solution 4 - Ios

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.

Solution 5 - Ios

Just turn off your Wi-Fi in Mac OS X. This works a treat!

Solution 6 - Ios

Download Additional tools package (Network Link Conditioner)

Description

Example in Sierra: enter image description here

enter image description here

Solution 7 - Ios

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.

Solution 8 - Ios

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: Ethernet Network Source

To turn off you Mac Wi-Fi Internet source (if your Mac is on a Wi-Fi Internet connection): Enter image description here

Solution 9 - Ios

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.

Solution 10 - Ios

iOS disable the network

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

https://i.stack.imgur.com/bCd1m.png" height="250">

*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.

Solution 11 - Ios

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.

Solution 12 - Ios

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
}

Solution 13 - Ios

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.

Solution 14 - Ios

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.

Solution 15 - Ios

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

  • Unplug your network connection
  • Turn Wi-Fi off

It's the simplest way.

Solution 16 - Ios

You could use OHHTTPStubs and stub the network requests to specific URLs to fail.

Solution 17 - Ios

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

Solution 18 - Ios

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.

Solution 19 - Ios

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.

Solution 20 - Ios

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.

Solution 21 - Ios

You could disable the network of the host instead!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaul HammondView Question on Stackoverflow
Solution 1 - IosprogrmrView Answer on Stackoverflow
Solution 2 - Iosswathy valluriView Answer on Stackoverflow
Solution 3 - IosNoah WitherspoonView Answer on Stackoverflow
Solution 4 - IosFelixView Answer on Stackoverflow
Solution 5 - IosbenpalmerView Answer on Stackoverflow
Solution 6 - IosNike KovView Answer on Stackoverflow
Solution 7 - Iosuser207616View Answer on Stackoverflow
Solution 8 - IosAliView Answer on Stackoverflow
Solution 9 - IosDionView Answer on Stackoverflow
Solution 10 - IosyoAlex5View Answer on Stackoverflow
Solution 11 - IosmabounassifView Answer on Stackoverflow
Solution 12 - IosDarshit ShahView Answer on Stackoverflow
Solution 13 - IosEran GoldinView Answer on Stackoverflow
Solution 14 - IosbrandenbyersView Answer on Stackoverflow
Solution 15 - IosDaniel BeltramiView Answer on Stackoverflow
Solution 16 - IosmwhussView Answer on Stackoverflow
Solution 17 - Iosut9081View Answer on Stackoverflow
Solution 18 - IosiGoreView Answer on Stackoverflow
Solution 19 - IosAstha TrivediView Answer on Stackoverflow
Solution 20 - IosPaul BView Answer on Stackoverflow
Solution 21 - IoswshcdrView Answer on Stackoverflow