Input type=file not working in WebView of OS X application
I have not worked with this myself, but from puzzling bits and pieces together this seems to do what you'd like.
First...I'm using a WKWebView
. That is declared and initialised with a local HTML file like so:
import Cocoa
import WebKit
class ViewController: NSViewController {
let webview: WKWebView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
webview.autoresizingMask = [.viewWidthSizable, .viewHeightSizable]
webview.frame = view.bounds
webview.uiDelegate = self
view.addSubview(webview)
let fileURL = URL(fileURLWithPath: "/Users/myuserhere/Desktop/index.html")
webview.loadFileURL(fileURL, allowingReadAccessTo: fileURL)
}
}
The interesting part is webview.uiDelegate
. This promises that we will conform to the WKUIDelegate
protocol documented here. As it says:
The WKUIDelegate class provides methods for presenting native user interface elements on behalf of a webpage.
One of the methods you can implement is runOpenPanelWithParameters:
If you implement this method, you promise that you will present a file upload panel and call the callback
method of this method with the outcome of what the user selected. Remember to also call the callback
method when the user presses cancel
.
Here is a quick and dirty example:
extension ViewController: WKUIDelegate {
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)
}
}
}
}
Hopefully that gives you something to get started with.
Bonus Material
Here are some links that helped me:
How to upload files from WKWebView
How to implement the delegate method
How to create a NSOpenPanel in Swift
For iOS (asked by @DarshanMothreja)
I tried gluing together a simple program to do the same on iOS. I hope it is useful to you @DarshanMothreja
HTML
A file called index.html
is added to the Xcode project. The content looks like this:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<label for="file">File goes here</label>
<input type="file" name="file" value="File">
</body>
</html>
How can you get input file to work in a WKWebView?
This is a known bug in WKWebView.
https://bugs.webkit.org/show_bug.cgi?id=137759
Upload file using input type=file with WKWebView does not open file dialog
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)
}
}
}
Related Topics
Get Url to a Local File with Spm (Swift Package Manager)
Error When Using Generic as Property Type in Swift
How to Rotate Object in a Scene with Pan Gesture - Scenekit
Swift Programmatically Set Segues
How to Make Protocol Associated Type Require Protocol Inheritance and Not Protocol Adoption
Which Swift Character Count Should I Use When Interacting with Nsstring APIs
Detecting If Wifi or Bluetooth Is Turned on or Off by the User
Scene Created in Sprite Kit Level Editor Is Not Working
How to Store a Reference to an Integer in Swift
Clgeocoder in Swift - Unable to Return String When Using Reversegeocodelocation
Example of Dispatch_Once in Swift
How to Make Apple Sign in Revoke Token Post Request
Detailed Instruction on Use of Nsopenpanel
How to Use Unsafemutablerawpointer to Fill an Array
How to Get All Days in Current Week in Swift
Using Guard with a Non-Optional Value Assignment