My app has a WebView. In this WebView, I want to upload image from photo Gallery or Camera.
I did it for Android using WebChromeClient. I have no idea how to do it in iOS using Objective-C.
My app has a WebView. In this WebView, I want to upload image from photo Gallery or Camera.
I did it for Android using WebChromeClient. I have no idea how to do it in iOS using Objective-C.
While user tap on upload image button on webview, from webview send a ScriptMessage
to iOS and in iOS catch that message on WKScriptMessageHandler
of WebKit
inside,
userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage)
Here message
contains the Script message from webview in message.body
. Then open camera or gallery according to message and take photo as native app in imagepickerDelegate
and save to appropriate API. You can refresh the web view after save as well.
Here is a SO post for more elaborate answer
Assuming your upload code is in JavaScript you can add the following into your HTML
<label for="fileupload">Upload</label>
<input id="fileupload" accept="image/*" style="display:none;" type="file">
Add a change event in js to the input field and upload your file. Here is the code in JQuery
$("#fileupload").on("change", function(value){
var value = $("#fileupload").get(0).files[0]
// clear file so if file is loaded a second time the change event is still triggered
$("#fileupload").val('')
// your code to upload the file
uploadFile(value)
})
The only other things you'll need to do are add the "Privacy - Camera Usage Description" and "Privacy - Photo Library Usage Description" flags to your app Info.plist file to enable your app's access to the camera and photolibrary.