472

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

Honey
  • 24,125
  • 14
  • 123
  • 212
Paul Hammond
  • 5,208
  • 3
  • 15
  • 10

20 Answers20

1874

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

progrmr
  • 71,681
  • 16
  • 107
  • 147
331

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.

user1046037
  • 14,608
  • 12
  • 79
  • 117
swathy valluri
  • 4,171
  • 2
  • 14
  • 12
  • 7
    if you are on the developersite it's in the package HARDWARE IO TOOLS FOR XCODE. – liquid Jul 21 '12 at 16:17
  • http://developer.apple.com/library/ios/#documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/xcode_4_3.html – Jerry Jul 24 '13 at 15:21
  • 186
    Somehow I prefer the idea of encasing my device in foil. – Michael Robinson Mar 06 '14 at 08:06
  • @race_carr Since the simulator shares the same connection, it actually works on the simulator, i.e., you can simulate those network scenarios for your app. – Ayberk Jul 21 '14 at 08:56
  • Has anyone managed to get notifications of network changes to work in the simulator with `Network Link Conditioner`? – fatuhoku Sep 08 '14 at 16:59
  • 1
    As of today (Feb 2015) this is packaged into "Hardware IO Tools for Xcode" – Mark Henderson Feb 23 '15 at 03:57
  • 2
    It is a good tool, but unfortunately I found out that the `SCNetworkReachabilityFlags` contains the `.Reachable` flag when using the "100% Loss" profile of the tool; as opposed to a real disconnected state. – Gobe Feb 15 '16 at 20:26
  • agreed with @Gobe unable to simulate a disconnected network with the tool – Ismail Iqbal Jan 16 '17 at 12:15
  • 16
    Since Xcode 8, the Network Link Conditioner is in the "[Additional Tools for Xcode](https://developer.apple.com/download/more/?name=Additional%20Tools%20for%20Xcode)" package. – ElegyD Mar 14 '17 at 14:43
  • 6
    Also, 100% loss throttles the host's connection so, not a practical solution. If you are prepared to develop without the internet then simply pulling the plug on your developer machine is better than this solution. – Jannie Theunissen May 16 '17 at 12:04
  • 12
    It is affecting all the mac. I want to quit the internet connection only for the simulator, because I am using the mac remotely ... – JCarlosR May 20 '17 at 20:01
  • Right now I'm on XCode 8.3.3 and they only have tools for 8.2 and 9 beta (1, 3, 4 and 6). I'll try the tools for 8.2 but still this is confusing. Why isn't this stuff simply update itself? At least whithin one major version like 8.x. – ecth Aug 28 '17 at 13:32
  • Reachability manager still show that I am connecting to network in case of 100% loose – Matrosov Alexander Aug 12 '19 at 11:35
  • 1
    As said before, this tool does not allow to control the connection of the simulator only, it controls the connection of the entire macos. – Vilmir Jan 03 '20 at 16:41
  • 1
    That doesn't solve the issue of testing offline apps. It affects the OS internet as well – Mariano J. Ponce Aug 18 '20 at 10:42
292

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.

Noah Witherspoon
  • 56,163
  • 16
  • 128
  • 131
  • marking as correct, as it answers the specific question I asked, but am also acknowledging the helpful answer from phix23. – Paul Hammond Jan 26 '11 at 18:49
  • 54
    Not sure if it's in response to this radar bug, but the Developer Tools now include a [Network Link Conditioner](http://mattgemmell.com/2011/07/25/network-link-conditioner-in-lion/) tool. – samvermette Apr 05 '12 at 18:12
  • 7
    Network Link Conditioner allows you to specify a percentage of packets to be dropped, but as far as I've seen there's unfortunately no way to disable all connectivity. – modocache Apr 09 '12 at 08:57
  • 11
    Currently Network Link Conditioner allows to set a 100% packet loss, and it seems to work. – Gabriele Petronella Oct 23 '13 at 19:40
  • 4
    Since the Simulator shares the host machine's network connection, you can turn off internet on your host machine to simulate "network disabled". Not ideal but it works. – Adil Hussain Jul 20 '16 at 16:32
  • Thanks for logging the bug report and encouraging others to actually do something about the problem. – Jannie Theunissen May 16 '17 at 11:56
  • 7
    Wait, Network Link Conditioner applies to the entire host machine, not just the simulator. How is this useful? I can just unplug my ethernet if I want that. – sudo May 24 '17 at 23:13
  • @samvermette - as sudo said, the Network Link Conditioner tool applies to the entire host machine, not just the simulator, right? – jbyrd Jan 29 '18 at 14:19
  • 1
    Any way to turn off the network while doing unit tests? https://stackoverflow.com/questions/46404535/xcuitest-how-to-disable-wi-fi-while-app-is-running?noredirect=1&lq=1 has an idea about how to do it for UI tests, but I'm testing my code not the UI. – Chris Prince May 24 '18 at 03:16
  • This might be a silly thought but, can you not just put the sim in Flight mode? – Dean Puckett Jul 23 '20 at 09:30
  • 1
    @DeanPuckett: I can't find such option in the simulator ... The control center is empty, in settings there is nothing ... – testing Mar 10 '21 at 09:05
49

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.

Hemang
  • 25,740
  • 17
  • 113
  • 171
Felix
  • 35,041
  • 12
  • 93
  • 141
21

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

benpalmer
  • 1,853
  • 3
  • 15
  • 21
  • 1
    Doesn't help the OP who specifically stated that this wasn't an option, but this is a perfect simple solution for me. – Johno Feb 28 '13 at 11:54
  • It's a problem when you're also hardwired then you have to disable both. Wifi is often enabled even when wired for things like AirPlay to work. – grego Jun 30 '20 at 17:27
18

Download Additional tools package (Network Link Conditioner)

Description

Example in Sierra: enter image description here

enter image description here

Nik Kov
  • 10,605
  • 4
  • 56
  • 96
  • 13
    This also throttles the host's connection, so it is no better than pulling the plug on your developer machine -- no collaboration or internet reference. So, not a practical solution. – Jannie Theunissen May 16 '17 at 11:59
11

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.

  • 1
    Just want to add, that if you create a custom network setting with 0 bandwidth, it automatically assumes it as a maximum. It's important to put 100% loss of packets. – Denis Kutlubaev Aug 29 '12 at 11:34
  • Also, I'd like to add, that this method removes internet from everything, not only simulator. May be it's even easier to just turn off internet completely. But nevertheless it's very handy tool since you can simulate E and 3G internet of different quality. – Denis Kutlubaev Aug 29 '12 at 11:35
  • This surely is the best answer because `Network Link Conditioner` is an official Apple tool, and it is totally free, compared to `Little Snitch` or `Hands Off`. – fatuhoku Sep 08 '14 at 16:33
  • Hmm, it looks like this tool does switch off network for everything. I also found that it doesn't trigger Reachability notifications (I used https://github.com/stefanomondino/STMReactiveReachability). – fatuhoku Sep 08 '14 at 16:57
  • 4
    BE Warned - The NETWORK LINK CONDITIONER tool impacts the ENTIRE machine, not just the iPhone Simulator. If you remote into your mac to for dev, like I do using the Xamarin Build Host, it will kill your connection. – John Kocktoasten Feb 10 '15 at 16:15
  • @JohnKocktoasten What tool do you use? I am also working remotely, and can't turn off the wifi only for the simulator. – JCarlosR May 21 '17 at 13:14
8

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

Ali
  • 996
  • 5
  • 20
  • 38
7

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.

mabounassif
  • 2,179
  • 6
  • 28
  • 44
6

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 
}
Darshit Shah
  • 2,200
  • 23
  • 33
  • 1
    I have a global class with a static function checking for the internet connection. Return ```false``` is a great idea. No installs, and I can continue working remotely! Thank you. – JCarlosR May 21 '17 at 14:18
  • Where is this `isInternetAvailable` defined?! – CSawy Aug 09 '19 at 23:01
  • @CSawy Its flag which send current status of internet connection in reachability class. – Darshit Shah Sep 06 '19 at 06:20
6

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.

Dion
  • 2,996
  • 16
  • 35
5

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.

Eran Goldin
  • 937
  • 12
  • 21
4

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

yoAlex5
  • 13,571
  • 5
  • 105
  • 98
3

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.

brandenbyers
  • 466
  • 4
  • 12
  • It may not work on iOS11GM. I can not find the Wifi setting in the simulator. – Bill Chan Sep 18 '17 at 23:52
  • 2
    Using Siri isn't the only option with Xcode 9 now that it is possible to activate multiple apps. Activating the iOS Settings app to turn of wifi now works too. – brandenbyers Sep 27 '17 at 21:06
3

There are two way to disable IOS Simulator internet:

  • Unplug your network connection
  • Turn Wi-Fi off

It's the simplest way

Daniel Beltrami
  • 571
  • 6
  • 16
2

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

mwhuss
  • 2,838
  • 1
  • 12
  • 7
1

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

ut9081
  • 315
  • 2
  • 7
0

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.

iGore
  • 119
  • 5
0

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.

-1

you could disable the network of the host instead!

wshcdr
  • 861
  • 2
  • 11
  • 25
  • 8
    It's inconvenient to not be able to see StackOverflow when debugging your app's connection problems! – fatuhoku Sep 08 '14 at 16:59
  • Read the last line of the question: "(I am connecting to the Mac remotely to code, no other choice right now, so disabling the OS network isn't an option)." Besides, your suggestion was already given two years ago, by benpalmer. – ToolmakerSteve Dec 06 '15 at 18:03