Swift / WKWebView Photo Upload Workaround
Fixing the iPhone bug where WKWebView's photo picker also dismisses the parent NavigationController.
~/posts/swift-wkwebview-photo-upload $ cat post.md
Keeping the picker from closing the whole nav stack
The extension below overrides dismiss on UINavigationController so
that <input type="file"> in a WKWebView can open the photo picker
without taking down its parent.
extension UINavigationController {
override open func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
if self.presentedViewController != nil {
super.dismiss(animated: flag, completion: completion)
}
}
}
The reason this is needed is a long-standing iOS bug. iPad is fine,
but on iPhone, when an <input type="file"> is tapped, a bottom sheet
appears with Camera / iCloud / Photo Library / Cancel. Inside
WKWebView, that sheet attaches to the WebView, not the
NavigationController — so the child NavigationController’s dismiss
doesn’t reach it.
Result: dismissing that sheet — whether by picking Camera, iCloud, Photo Library, or Cancel — tears down the entire outer NavigationController. The override above intercepts the unintended dismiss.