1

Is there a way to programmably create an effect like the iOS Network Link Conditioner for your own code, while the code is running?

The specific condition I want to test is "Step 1 of a login flow works, but network cuts out for Step 2". This is hard to test manually, as I guess you'd have to set a breakpoint, turn on Airplane Mode, then resume the app. This introduces observation error since the breakpoint affects the timing and control flow of the app.

Ideally you could set your own delay and dropout parameters similar to the actual Network Link Conditioner, but even just being able to switch network on and off at specific times would be enough.

bcattle
  • 12,115
  • 6
  • 62
  • 82

2 Answers2

0

The best way to do that is probably to use a mock object in place of NSURLSession that either passes data through to the real NSURLSession class or generates fake errors. For example, in your prefix header, you could add a conditional #define that replaces NSURLSession with a different class name and then #undef it in the header and implementation files for your wrapper class.

You could also turn the real NSURLSession class into a mock object by swizzling the various methods that your app calls and wrapping them with custom logic.

If you decide to go with the swizzling approach, then I would suggest reading https://blog.newrelic.com/2014/04/16/right-way-to-swizzle/ to learn how to avoid common mistakes while swizzling (though I guess for testing code that will never ship, that's somewhat less critical).

dgatwood
  • 10,129
  • 1
  • 28
  • 49
0

Now accessible directly inside Xcode 14, no need to add any tools for that.

enter image description here

Saurabh Sharma
  • 1,671
  • 2
  • 13
  • 16