簡體   English   中英

使用 WKWebView 的輸入類型 = 文件上傳文件不會打開文件對話框

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

我正在使用 Xcode 11.5 並在 WKWebView 中加載 web 頁面。 我將文件訪問權限設置為只讀,這是我的應用程序功能列表。

在此處輸入圖像描述

這就是我的loadView的樣子

    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
    }

當我在 iOS 上使用 Safari 並選擇輸入類型 = 文件時,對話框打開,但使用我的 macOS 應用程序使用 webview 它不起作用? 我需要在我的應用程序上設置更多功能嗎? 我發現了一些文件輸入的老問題,但它們似乎已經解決了?

像這樣實現了 UIDelegate。

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)
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM