Detect user input in PDF form fields on iOS

Form elements are based on annotations, so you can use Notification.Name.PSPDFAnnotationChanged to listen for user input in forms:

// Start listening to the change notifications from all annotations.
NotificationCenter.default.addObserver(self, selector: #selector(annotationChanged(_:)), name: .PSPDFAnnotationChanged, object: nil)
@objc func annotationChanged(_ notification: Notification) {
// Make sure it was a text field that changed.
guard let annotation = notification.object as? TextFieldFormElement else {
return
}
// Make sure that the annotation is in the currently handled document.
guard annotation.document === self.document else {
return
}
// Make sure that the contents, and not the style, changed.
guard let keyPaths = notification.userInfo?[PSPDFAnnotationChangedNotificationKeyPathKey] as? [String], keyPaths.contains(#keyPath(Annotation.contents)) else {
return
}
// Handle the change.
print("Text field content changed: \(annotation.contents!)")
}

The technique above can also be used to listen for changes in other types of form fields.