14

Apple wants me to give the user a friendly apology if I can't find an internet connection. Using the Reachability Demo, this was easy enough. I want to take it a step further and monitor for a connection loss. The demo has this functionality, but I can't figure out how to shut my connection off to test if it works.

How would I go about simulating a loss of (or actually losing) a connection?

7 Answers 7

19

If you're developing in the iPhone Simulator, simply disconnect your computer from the internet. If your computer has no network access, neither does the iPhone Simulator.

To test on the device, you can do the following:

First, setup a WiFi router that you can use for testing.

You can turn on Airplane mode on the phone, then turn WiFi back on. This will prevent the 3G connection from providing data access.

Launch your application. Now you can simply disconnect your WiFi router to simulate a loss of connectivity.

2
  • 2
    I normally wear a tin hat, so the cage is a natural progression. heh Thanks Joey, I wanted to test on the device. Cheers.
    – Joel Hooks
    Jul 3, 2009 at 4:43
  • 2
    You can put your phone in the microwave to simulate a real-life connection loss... Just remember not to turn the microwave on!
    – Sam
    May 15, 2012 at 13:22
13

You can use the SpeedLimit preference pane to simulate network latency under the simulator. And here's a command line version built on top of the ipfw command.

The advantage over just yanking the cord or killing WiFi is that you can specify the speed when hitting specific hosts so it can be used for testing without killing your regular network services.

1
  • Thanks, the SpeedLimit thing is super useful.
    – Tom Redman
    Dec 8, 2011 at 20:08
12

You could install Apple's Network Link Conditioner

On Yosemite:

Network Link Conditioner can be found in the "Hardware IO Tools for Xcode" package. This can be downloaded from the Apple Developer Downloads page. Once the download has finished, open the DMG and double-click "Network Link Condition.prefPane" to install (source).

With this preferences panel you could create a profile with 0kbps speed.

Network Link Conditioner

On older versions of OSX:

Mountain Lion / Mavericks: Xcode > Open Developer Tool > More Developer Tools
Lion: /Developer/Applications/Utilities/Network Link Conditioner

2
  • 4
    Note: Starting with Xcode 4.3 (or 4.2?) these must be installed separately from Apple's web site: in Xcode, goto "Xcode / Open Developer Tools / More Developer Tools...". This will take you to the download section. The link conditioner can be found in the "Hardware IO Tools for Xcode".
    – Stefan
    Apr 23, 2012 at 18:44
  • Good but even if you create a profile with 0 values, it doesn't simulate the "no connection" situation...
    – gemini
    Jan 28, 2013 at 11:19
4

For testing in the simulator I make great use of Little Snitch. It's a very useful application for writing rules as to what can communicate with the outside world and for how long.

It will also notify you if an application wishes to access the internet on the fly and you can temporarily restrict access for either that time, that session or forever.

As for testing on the device, I log into my wireless router and temporarily disable either the network or turn on restriction by MAC ID for the duration of the test.

4

Use Charles Web Proxy You can inspect all HTTP/S requests your app sends and responses it receives, throttle connection to simulate any network speed. Create custom throttle profile called "Disconnected" with Bandwidth = 0, to simulate network disconnections.

1
  • Bandwidth 0 is not a disconnect as far as reachability is concerned. If you use it as a proxy for your device, the device will still think it has a good connection to the wifi network, so reachability will not return failure. 0 bandwidth is still a useful tool for testing network stalls.
    – CornPuff
    Jan 31, 2013 at 21:14
2

Perhaps this preference pane is also useful for you: SpeedLimit.prefPane it can't loose the connection, but you can slow down it based on the host and port you're trying to reach.

0

Searching for similar need i.e. simulating a lossy wifi network connection on real device, I figured out how to do it :) I connected my MacBook Pro on ethernet and shared my Wifi connection to the device. Using Apple's Network Link Conditioner Prefpane then let me played with the connection quality. Very useful to simulate 3G, Edge and other baudrate.

Next step : find and buy a usb wifi adapter working on MAC OS X to let all of us (developpers team) to test without wiring each computer to ethernet.

Hope this will help some of you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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