[ios] 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 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 any way of disabling the network in the simulator?

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

This question is related to ios iphone debugging networking ios-simulator

The answer is


Just turn off your WiFi in Mac OSX this works a treat!


You can throttle the internet connection with a 3rd party app such as

Charles: http://www.charlesproxy.com/

Hit command + shift + T on a Mac to setup the throttling.


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


I would suggest you using Charles Proxy app on Mac

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

Star/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 thought your computer


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 in your local network... For the application it will be the same as if there was no internet connection or the server is 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 webserver on your local machine.


If you have at least 2 wifi networks to connect is a very simple way is to use a bug in iOS simulator:

  1. quit from simulator (cmd-q) if it is open
  2. connect your Mac to one wifi (it may be not connected to internet, no matters)
  3. launch simulator (menu: xCode->Open Developer Tool->iOs Simulator) and wait while it is loaded
  4. switch wifi network to other one
  5. profit

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

Until you relaunched simulator- it will have no internet (even if that first wifi network you connected had internet connection), so you can run (cmd-R) and stop (cmd-.) project(s) to use simulator without connection, but your Mac will be connected.

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


There are two way to disable IOS Simulator internet:

  • Unplug your network connection
  • Turn Wi-Fi off

It's the simplest way


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 wifi to no network.

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


Yes. In Xcode, you can go to Xcode menu item -> Open Developer Tools -> More 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.


With Xcode 8.3 and iOS 10.3:

XCUIDevice.shared().siriService.activate(voiceRecognitionText: "Turn off wifi")
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 wifi 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.


The only way to disable network on iOS simulator I know is using tools like Little Snitch or Hands Off. With them you can deny/block any out- and ingoing network connections. You can set it up so that it only blocks connections from the simulator app. Works like a firewall.


One probably crazy idea or patch :

Just toggle the flag of network reachability

This is code which I use to toggle my flag runtime by triggering 'Simulator Memory Warning' and its COMPLETELY SAFE, just make sure code should be in DEBUG Mode only

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

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


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


Download Additional tools package (Network Link Conditioner)

Description

Example in Sierra: enter image description here

enter image description here


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.


Just updating the answer to the current date. Since Xcode 4 (?) there is a preferences pane in /Applications/Utilities called Network Link Conditioner. Either you use one of the existent profiles or you create your own custom profile with 0 Kbps Up/Downlink and 100% dropped.


Since Xcode does not provide such 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 from "System Preferences..." > "Network" and turn off the desire network source.

To turnoff you MAC Ethernet internet source: EtherNet Network Source

To turnoff you MAC WiFi internet source(if your MAC is on Wifi Internet): enter image description here


you could disable the network of the host instead!


Examples related to ios

Adding a UISegmentedControl to UITableView Crop image to specified size and picture location Undefined Symbols error when integrating Apptentive iOS SDK via Cocoapods Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Autoresize View When SubViews are Added Warp \ bend effect on a UIView? Speech input for visually impaired users without the need to tap the screen make UITableViewCell selectable only while editing Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture arm64

Examples related to iphone

Detect if the device is iPhone X Xcode 8 shows error that provisioning profile doesn't include signing certificate Access files in /var/mobile/Containers/Data/Application without jailbreaking iPhone Certificate has either expired or has been revoked Missing Compliance in Status when I add built for internal testing in Test Flight.How to solve? cordova run with ios error .. Error code 65 for command: xcodebuild with args: "Could not find Developer Disk Image" Reason: no suitable image found iPad Multitasking support requires these orientations How to insert new cell into UITableView in Swift

Examples related to debugging

How do I enable logging for Spring Security? How to run or debug php on Visual Studio Code (VSCode) How do you debug React Native? How do I debug "Error: spawn ENOENT" on node.js? How can I inspect the file system of a failed `docker build`? Swift: print() vs println() vs NSLog() JavaScript console.log causes error: "Synchronous XMLHttpRequest on the main thread is deprecated..." How to debug Spring Boot application with Eclipse? Unfortunately MyApp has stopped. How can I solve this? 500 internal server error, how to debug

Examples related to networking

Access HTTP response as string in Go Communication between multiple docker-compose projects Can't access 127.0.0.1 How do I delete virtual interface in Linux? ConnectivityManager getNetworkInfo(int) deprecated Bridged networking not working in Virtualbox under Windows 10 Difference between PACKETS and FRAMES How to communicate between Docker containers via "hostname" java.net.ConnectException: failed to connect to /192.168.253.3 (port 2468): connect failed: ECONNREFUSED (Connection refused) wget: unable to resolve host address `http'

Examples related to ios-simulator

The iOS Simulator deployment targets is set to 7.0, but the range of supported deployment target version for this platform is 8.0 to 12.1 flutter run: No connected devices Error Running React Native App From Terminal (iOS) How to uninstall downloaded Xcode simulator? Capture iOS Simulator video for App Preview iPhone 6 Plus resolution confusion: Xcode or Apple's website? for development How to enable native resolution for apps on iPhone 6 and 6 Plus? How to access iOS simulator camera Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." Document directory path of Xcode Device Simulator