textView(_, shouldChangeTextIn, replacementText)
can be called in many cases. if you will read UIPasteboard.general.string
value every time, user will see system alert that application accessed pasteboard after every letter typed.
To avoid that problem you can create subclass of UITextView and override paste(_)
function in it. This function is being called by system once every time when user tries to paste something.
Use next call of textView(_, shouldChangeTextIn, replacementText)
to handle paste event.
var isPastingContent = false // helper variableopen override func paste(_ sender: Any?) { isPastingContent = true // next call of `shouldChangeTextIn` will be for the paste action super.paste(sender)}func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { if isPastingContent { // if we detected paste event // paste detected, do what you want here let pasteboardContent = UIPasteboard.general.string // you can get pasteboard content here safely. System alert will be shown only once. isPastingContent = false // toggle helper value back to false }}