marcus
marcus

Reputation: 10096

Upload file using input type=file with WKWebView does not open file dialog

I'm using Xcode 11.5 and loading a web page in a WKWebView. I have file access set to read only and this is my apps capability list.

enter image description here

This is how my loadView look like

    override func loadView() {
        //Inject JS string to read console.logs for debugging
        let configuration = WKWebViewConfiguration()
        let action = "var originalCL = console.log; console.log = function(msg){ originalCL(msg); window.webkit.messageHandlers.iosListener.postMessage(msg); }" //Run original console.log function + print it in Xcode console
        let script = WKUserScript(source: action, injectionTime: .atDocumentStart, forMainFrameOnly: false) //Inject script at the start of the document
        configuration.userContentController.addUserScript(script)
        configuration.userContentController.add(self, name: "iosListener")
        
        //Initialize WKWebView
        webView = WebView(frame: (NSScreen.main?.frame)!, configuration: configuration)
        
        //Set delegates and load view in the window
        webView.navigationDelegate = self
        webView.uiDelegate = self
        view = webView
    }

When I use Safari on iOS and tap the input type=file, the dialog opens up, but using my macOS app with a webView it does not work. Do I need to set more capabilities on my app? I found some old issues with file inputs but they seem to be resolved?

Upvotes: 2

Views: 5505

Answers (1)

marcus
marcus

Reputation: 10096

Implemented the UIDelegate like this.

func webView(_ webView: WKWebView, runOpenPanelWith parameters: WKOpenPanelParameters, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping ([URL]?) -> Void) {
    let openPanel = NSOpenPanel()
    openPanel.canChooseFiles = true
    openPanel.begin { (result) in
        if result == NSApplication.ModalResponse.OK {
            if let url = openPanel.url {
                completionHandler([url])
            }
        } else if result == NSApplication.ModalResponse.cancel {
            completionHandler(nil)
        }
    }
}

Upvotes: 4

Related Questions