588

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

2
  • I think you can also use CharlesProxy to run similar tests
    – mfaani
    Sep 24, 2019 at 12:59
  • One more option is simply returning false in network check util function.
    – jeff ayan
    Nov 17, 2022 at 10:32

21 Answers 21

2215

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

11
  • 283
    Just a quick suggestion, if you create a walk-in Faraday cage with a desk inside, the Mac will be much easier to work with. Apr 15, 2013 at 17:21
  • 146
    your answer is interesting even though he asked for simulator May 30, 2013 at 12:22
  • 149
    Since the question was about the simulator, I think I'll just make one of these in Photoshop and set it as my desktop background. That will probably work, right? Oct 26, 2014 at 23:58
  • 16
    This 'trick' has been used for real by professionals :) Back when I worked on the first gen smart phones we used to use stout biscuit tins. Not all brands worked, but those that did were perfect.
    – Will
    Dec 15, 2015 at 14:19
  • 21
    should be accepted as correct answer! ( I'd rather use plumbum box for iKryptonite ) Mar 4, 2016 at 20:41
428

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.

16
  • 8
    if you are on the developersite it's in the package HARDWARE IO TOOLS FOR XCODE.
    – liquid
    Jul 21, 2012 at 16:17
  • 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, 2016 at 20:26
  • 20
    Since Xcode 8, the Network Link Conditioner is in the "Additional Tools for Xcode" package.
    – ElegyD
    Mar 14, 2017 at 14:43
  • 10
    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. May 16, 2017 at 12:04
  • 19
    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, 2017 at 20:01
306

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.

11
  • 56
    Not sure if it's in response to this radar bug, but the Developer Tools now include a Network Link Conditioner tool. Apr 5, 2012 at 18:12
  • 8
    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. Apr 9, 2012 at 8:57
  • 11
    Currently Network Link Conditioner allows to set a 100% packet loss, and it seems to work. Oct 23, 2013 at 19:40
  • 6
    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. Jul 20, 2016 at 16:32
  • 14
    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, 2017 at 23:13
50

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.

5
  • 15
    but that won't make the simulator behave like you're offline. It will just make your servers be unavailable, which is quite a different thing.
    – Blitz
    Jan 26, 2011 at 18:45
  • 1
    actually, LordT, that may be sufficient for the specific circumstances I have...! Jan 26, 2011 at 18:48
  • phix23 - do you have a preference between those two suggested tools? Jan 26, 2011 at 18:55
  • No I haven't. I've only tried Hands off, because it has more features.
    – Felix
    Jan 26, 2011 at 19:01
  • 2
    +1 : I've used Little Snitch for exactly this purpose, testing to make sure a Simulated app works if the network connection is broken.
    – hotpaw2
    Jan 26, 2011 at 19:41
28

Download Additional tools package (Network Link Conditioner)

Description

Example in Sierra: enter image description here

enter image description here

3
  • 25
    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. May 16, 2017 at 11:59
  • 1
    @JannieTheunissen it seems you prefer faraday cage:) this is the answer for the question and it works.
    – Nike Kov
    Sep 1, 2023 at 16:06
  • 1
    you have a great answer and upvote from me. My comment is only helpful for folk with a mac (like me who came here looking for a solution), but for the specific case of the OP - salute! Sep 2, 2023 at 17:26
25

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

2
  • 2
    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, 2013 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, 2020 at 17:27
15

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

0
10

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.

0
9

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.

6
  • 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. Aug 29, 2012 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. Aug 29, 2012 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 8, 2014 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 github.com/stefanomondino/STMReactiveReachability).
    – fatuhoku
    Sep 8, 2014 at 16:57
  • 7
    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. Feb 10, 2015 at 16:15
8

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

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

7

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.

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.

5

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
}
3
  • 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, 2017 at 14:18
  • 1
    Where is this isInternetAvailable defined?!
    – CSawy
    Aug 9, 2019 at 23:01
  • @CSawy Its flag which send current status of internet connection in reachability class. Sep 6, 2019 at 6:20
3

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.

2
  • 1
    It may not work on iOS11GM. I can not find the Wifi setting in the simulator.
    – Bill Chan
    Sep 18, 2017 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. Sep 27, 2017 at 21:06
2

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

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

1

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.

1

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

  • Unplug your network connection
  • Turn Wi-Fi off

It's the simplest way.

1
  • 8
    Welcome to kindergarten
    – Farid
    Jan 13, 2023 at 15:54
1

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.

0

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.

-2

You could disable the network of the host instead!

3
  • 10
    It's inconvenient to not be able to see StackOverflow when debugging your app's connection problems!
    – fatuhoku
    Sep 8, 2014 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. Dec 6, 2015 at 18:03
  • There literally 5 more answers just like this one but they all have at least 2 upvotes. Looks like you are just unlucky, LOL
    – Farid
    Jan 13, 2023 at 15:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.