Fix UICompatibilityInputViewController weak reference crash with defensive nil-checking #2136
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR addresses a crash that occurs when the iOS system attempts to form weak references to view controllers that are in the process of deallocation:
While
UICompatibilityInputViewControlleris not directly part of IQKeyboardManager, the library's view controller hierarchy traversal and weak reference management can trigger this crash when UIKit's internal keyboard input system is transitioning or deallocating view controllers.Root Cause
The crash occurs during view controller lifecycle transitions when:
Solution
Added comprehensive defensive programming measures to prevent accessing deallocated objects:
Enhanced Weak Reference Handling
rootControllerin Combine publisher sink to prevent retain cyclesrootControllerhasn't been deallocated before accessing itrootConfigurationstill references the same controller instanceStrengthened Configuration Validation
IQRootControllerConfigurationwith explicit nil checksImproved View Controller Hierarchy Navigation
iq_parentContainerViewController()Impact
These changes prevent the "Cannot form weak reference" crash by ensuring that:
The fix is minimal and surgical, focusing purely on defensive programming without altering the library's core functionality or public API.
Fixes #2106.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.