ChatGPT解决这个技术问题 Extra ChatGPT

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

I think you can also use CharlesProxy to run similar tests

P
Peter Mortensen

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

https://i.stack.imgur.com/w6ehv.png


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.
your answer is interesting even though he asked for simulator
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?
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.
should be accepted as correct answer! ( I'd rather use plumbum box for iKryptonite )
P
Peter Mortensen

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.


if you are on the developersite it's in the package HARDWARE IO TOOLS FOR XCODE.
Somehow I prefer the idea of encasing my device in foil.
Since Xcode 8, the Network Link Conditioner is in the "Additional Tools for Xcode" package.
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.
It is affecting all the mac. I want to quit the internet connection only for the simulator, because I am using the mac remotely ...
N
Noah Witherspoon

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.


Not sure if it's in response to this radar bug, but the Developer Tools now include a Network Link Conditioner tool.
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.
Currently Network Link Conditioner allows to set a 100% packet loss, and it seems to work.
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.
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.
P
Peter Mortensen

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.


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.
actually, LordT, that may be sufficient for the specific circumstances I have...!
phix23 - do you have a preference between those two suggested tools?
No I haven't. I've only tried Hands off, because it has more features.
+1 : I've used Little Snitch for exactly this purpose, testing to make sure a Simulated app works if the network connection is broken.
P
Peter Mortensen

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


Doesn't help the OP who specifically stated that this wasn't an option, but this is a perfect simple solution for me.
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.
N
Nike Kov

Download Additional tools package (Network Link Conditioner)

Description

https://i.stack.imgur.com/Z81B2.png

https://i.stack.imgur.com/lTAIK.png


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.
P
Peter Mortensen

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.

https://i.stack.imgur.com/b4aQr.png

https://i.stack.imgur.com/8iXqt.png


P
Peter Mortensen

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.


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.
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.
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.
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).
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.
P
Peter Mortensen

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.


nice, great hack!
y
yoAlex5

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.


P
Peter Mortensen

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.


P
Peter Mortensen

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
}

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.
Where is this isInternetAvailable defined?!
@CSawy Its flag which send current status of internet connection in reachability class.
P
Peter Mortensen

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.


P
Peter Mortensen

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.


It may not work on iOS11GM. I can not find the Wifi setting in the simulator.
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.
P
Peter Mortensen

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

Unplug your network connection

Turn Wi-Fi off

It's the simplest way.


m
mwhuss

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


u
ut9081

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

credit for this solution: http://hints.macworld.com/article.php?story=20130325002258846


P
Peter Mortensen

If you have at least two Wi-Fi networks to connect is a very simple way is to use a bug in iOS simulator:

quit from the simulator (Cmd + Q) if it is open connect your Mac to one Wi-Fi access point (it may be not connected to the Internet, but it doesn't matter) launch the simulator (menu: Xcode → Open Developer Tool → iOS Simulator) and wait while it is loaded switch Wi-Fi network to the other one 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.


P
Peter Mortensen

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.


P
Paul B

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.


P
Peter Mortensen

You could disable the network of the host instead!


It's inconvenient to not be able to see StackOverflow when debugging your app's connection problems!
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.