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
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:
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:
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:
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 ;-)
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:
To turnoff you MAC WiFi internet source(if your MAC is on Wifi Internet):
you could disable the network of the host instead!
Source: Stackoverflow.com