【发布时间】:2019-04-03 05:29:17
【问题描述】:
我的应用有一个 WebView。在这个 WebView 中,我想从相册或相机上传图片。
我使用 WebChromeClient 为 Android 做的。我不知道如何在 iOS 中使用 Objective-C 来做到这一点。
【问题讨论】:
标签: ios objective-c xcode uiwebview
我的应用有一个 WebView。在这个 WebView 中,我想从相册或相机上传图片。
我使用 WebChromeClient 为 Android 做的。我不知道如何在 iOS 中使用 Objective-C 来做到这一点。
【问题讨论】:
标签: ios objective-c xcode uiwebview
当用户点击 webview 上的上传图片按钮时,从 webview 向 iOS 发送 ScriptMessage 并在 iOS 中在 WebKit 的 WKScriptMessageHandler 上捕获该消息,
userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage)
这里message 包含来自message.body 中webview 的脚本消息。然后根据消息打开相机或图库,并在imagepickerDelegate中将照片作为本机应用程序并保存到适当的API。您也可以在保存后刷新网页视图。
Here 是一个 SO 帖子以获得更详细的答案
【讨论】:
假设您的上传代码是 JavaScript,您可以在 HTML 中添加以下代码
<label for="fileupload">Upload</label>
<input id="fileupload" accept="image/*" style="display:none;" type="file">
在输入字段中添加 js 中的更改事件并上传您的文件。这是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)
})
您需要做的唯一其他事情是在您的应用 Info.plist 文件中添加“隐私 - 相机使用说明”和“隐私 - 照片库使用说明”标志,以使您的应用能够访问相机和照片库.
【讨论】: