This is the only way that I was able to get it to work. I used a textField but the same concept should still work for a textView.
In the shouldChangeCharactersIn
delegate method below I casted the string
argument to a NSString, then back to a String. Then I compared it to whatever was pasted. Everything else is in the comments above the code.
// 1. class property for anything that was copied and will be pastedvar pasted: String?// 2. When the user first taps the textfield set the above class property to the copied text (if there is any)func textFieldDidBeginEditing(_ textField: UITextField) { // 3. set it here pasted = UIPasteboard.general.string // this is what was copied and will be pasted}func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { guard let safeText = textField.text else { return true } let currentString: NSString = safeText as NSString let newString: NSString = currentString.replacingCharacters(in: range, with: string) as NSString let str = newString as String // 4. compare the above str constant to the pasted variable if str == self.pasted { print("pasted") } else { print("typed") } return true}func textFieldDidEndEditing(_ textField: UITextField) { // 5. when the user is finished with the textField set the pasted property to nil pasted = nil}