back to index

Swift / WKWebView Photo Upload Workaround

Fixing the iPhone bug where WKWebView's photo picker also dismisses the parent NavigationController.

published Jun 26, 2017 tags #swift #ios #webview

~/posts/swift-wkwebview-photo-upload $ cat post.md

/ LANG EN / 中文
/ THEME / /

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.

back to index