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