UITapGestureRecognizer Programmatically trigger a tap in my view

There is a much simpler way to trigger a touch for a UITapGestureRecognizer in a unit test using a single line. Assuming you have a var that holds a reference to the tap gesture recognizer all you need is the following:

singleTapGestureRecognizer?.state = .ended

I think you have multiple options here:

  1. May be the simplest would be to send a push event action to your view but i don't think that what you really want since you want to be able to choose where the tap action occurs.

    [yourView sendActionsForControlEvents: UIControlEventTouchUpInside];

  2. You could use UI automation tool that is provided with XCode instruments. This blog explains well how to automate your UI tests with script then.

  3. There is this solution too that explain how to synthesize touch events on the iPhone but make sure you only use those for unit tests. This sounds more like a hack to me and I will consider this solution as the last resort if the two previous points doesn't fulfill your need.