Gianluca Cesari Gianluca Cesari - 9 months ago 68
iOS Question

Programmatically press a keyboard button

I was wondering if there was a way to open the iOs keyboard and programmatically press a button without interacting with the keyboard itself.


Yes, there is a way. There are actually several ways but all of them are undocumented and you definitely should not used them in production.

Generating a tap means generating a UIEvent (several of them, actually) and passing it to UIApplication.sendEvent(_:).

Unfortunately, UIEvent inner structure is undocumented but some people have already inspected it and you can google tools to synthesize events (e.g. However note that the internal structure of UIEvent and UITouch can change between iOS versions (see iOS 9 UIEvent)

You can also use the internal UIAutomation API (see for example these generated headers) to generate a tap (that's what UI tests were using in previous releases) but again, this API is not publicly documented and it is loaded into the app only if the app is connected to Xcode. I am not sure how this private framework looks like on iOS 9.