In iOS apps, UITextField
can be used to receive text input from users.
Users can paste text into the UITextField
instead of typing directly via the keyboard.
Here’s the sequence of operations that occur during pasting:
Logic Sequence for Pasting
- When pasting, the first method called is
shouldChangeCharactersIn
, a part of the UITextFieldDelegate
.- At this point, you can block pasting if any unwanted characters are detected through regex validation.
- The
editingChanged
event then receives the input text.- You can either display the received text as-is or process it before displaying.
How to Prevent Pasting
class CustomTextField: UITextField {
open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.paste(_:)) {
return false
}
return super.canPerformAction(action, withSender: sender)
}
...
You can prevent various actions, including pasting, by implementing the UIResponderStandardEditActions
protocol.
🙋🏻♂️ Other Notes
- I attempted to use Cursor AI for tasks like converting an iOS project to a Flutter project or refactoring to a specific architectural pattern. My impression is that it understands SwiftUI code better than UIKit. It handles simple tasks like removing libraries such as SnapKit and Then and refactoring to basic implementations relatively well, but it’s not yet reliable enough for full architectural refactoring.
- I explored shortcuts and methods for writing articles after moving to Medium as my blogging platform.
- React Native’s Code Push will officially be retired in March 2025. Since Flutter now offers ShoreBird for code push functionality, Flutter seems to have an edge in this area as both platforms now require paid services.