123

iphone - Is it possible to disable the network in iOS Simulator? - Stack Overflo...

 6 years ago
source link: https://stackoverflow.com/questions/4808433/is-it-possible-to-disable-the-network-in-ios-simulator/13831212#13831212
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Is it possible to disable the network in iOS Simulator?

Asked 11 years, 4 months ago
Modified 2 months ago
Viewed 288k times

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

asked Jan 26, 2011 at 18:30

21 Answers

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
answered Dec 12, 2012 at 1:12

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.

answered May 31, 2012 at 14:38

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.

answered Jan 26, 2011 at 18:37

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.

answered Jan 26, 2011 at 18:41

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

answered May 12, 2011 at 9:21

Download Additional tools package (Network Link Conditioner)

Description

Example in Sierra:

enter image description here

enter image description here
answered Mar 16, 2017 at 19:22

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.

answered Mar 1, 2012 at 9:52

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
answered Feb 24, 2016 at 14:11

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.

answered Mar 18, 2019 at 11:22

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

bCd1m.png

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

answered May 11, 2020 at 15:27

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.

answered Nov 12, 2013 at 23:44

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
}
answered Sep 8, 2016 at 11:31

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.

answered Feb 9, 2016 at 15:16

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.

answered Mar 27, 2017 at 23:22

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

  • Unplug your network connection
  • Turn Wi-Fi off

It's the simplest way.

answered Sep 29, 2018 at 14:10

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

answered May 14, 2014 at 19:55

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

answered Jul 18, 2019 at 10:51

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.

answered Jan 28, 2015 at 11:44

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.

answered Feb 13, 2016 at 21:14

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.

answered Mar 20 at 10:37

You could disable the network of the host instead!

answered Jun 3, 2013 at 4:38
Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.

Not the answer you're looking for? Browse other questions tagged ios iphone debugging networking ios-simulator or ask your own question.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK