Simulating key press event to type text in UITextField

in iOS, user can set focus on UItextField and tapping a key in the virtual keyboard updates the text in the textfield. This user action causes the relevant delegates of UITextFieldDelegate to get invoked, i.e the handlers associated with action of user entering some text in the textfield.

I m trying to simulate this user action where I am trying to do this programatically. I want to simulate it in a way such that all the handlers/listeners which otherwise would have been invoked as a result of user typing in the textfield should also get invoked now when i am trying to do it programatically. I have a specific usecase of this in my application.

Below is how I m performing this simulation.

  1. I m manually updating the text field associated(UITextField.text) and updating its value.
  2. And then I m invoking the delegate manually as textField.delegate?.textField?(textField, shouldChangeCharactersIn: nsRange, replacementString: replacementString)

I wanted to know If this is the right way to do this. Is there something better available that can be used, such that simulation has the same affect as the user performing the update?

Answered by DTS Engineer in 835717022

For an app feature that works in the production environment, there is no way to programmatically create / post a key stroke event that mimics a key input a user does with the iOS virtual keyboard.

If you are working on test automation with XCUITest, typeText(_:) should help.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Update: I want to programatically simulate the key press event similar to user actually pressing a key on a virtual keyboard or external keyboard(for ipad). This will automatically invoke the UITextFieldDelegate method shouldChangeCharactersIn(_:range:replacementString:) and the pressesBegan(_:with:) methods, and take care of the textfield value updation.

Note : In MacOS we can create custom NSEvents and post them to NSApp, allowing us to simulate mouse, keypress, gesture interactions. I wanted to understand If there is something similar frame available for iOS simulation?

For an app feature that works in the production environment, there is no way to programmatically create / post a key stroke event that mimics a key input a user does with the iOS virtual keyboard.

If you are working on test automation with XCUITest, typeText(_:) should help.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

@DTS Engineer ,

Thank you for the earlier response. I understand that Apple’s position is that one cannot programmatically generate a real keystroke event that passes through the OS’s full event pipeline (pressesBegan, etc.). However, based on my experiments, I observe that: *

  • OverridinginsertText(_:) / deleteBackward()allow's interception of typed input and the possibility of deciding whether or not to call super so that the text in the text view actually updates.
  • Calling insertText() programmatically results in the same updating behavior as user typing (for display, selection, etc.)

Given these capabilities, could you clarify:

  • Is using insertText(_:) / deleteBackward() in a subclass considered an “allowed” way to simulate user text input, for production code (outside of testing)? Or is it officially unsupported / fragile behavior (i.e. subject to change)?
  • Even though we can’t generate a “real” key press event, since calling insertText(_:) does trigger the same downstream behaviors (delegate calls, text updates, cursor management), is this considered a supported and intended way to simulate user typing in production? Or is it just an internal side-effect that Apple does not want us to rely on?

insertText(_:) and deleteBackward() are part of UITextInput, and hence are public methods that you can use as needed.

The implementation of the methods in UITextField includes some important details, such as the interaction with the input delegate and text storage, and so be sure to call super's implementation if you would like to override the methods.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Simulating key press event to type text in UITextField
 
 
Q