Search code examples
cocoawebkitwebviewappkitnsundomanager

Removing undo actions for a WebView's NSUndoManager


I have several WebViews that I swap in and out of my view hierarchy. Some of these WebViews contain form fields, so I implement the editingDelegate to provide the NSUndoManager for my document.

The problem is that any typing done in the WebView generates actions that are placed on the undo stack. After the view is removed from the hierarchy, the actions are still in the undo stack. At that point, if a user uses Cmd-Z to "Undo Typing", an exception is thrown because -undoEditing: is being sent to a deallocated instance of WebEditorUndoTarget. (A private class used in the WebKit implementation.)

I can't use -removeAllActionsWithTarget: because I can't reference the target. It appears the only solution is to disable undo registration for the WebView.

Am I missing something or is this a limitation of a WebView?


Solution

  • One private API solution is to invoke -[WebView _clearUndoRedoOperations]. Haven't found a public API solution yet.