栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在wkwebview中启用摄像头和麦克风访问

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在wkwebview中启用摄像头和麦克风访问

是的,看看cordova-plugin-iosrtc和cordova-plugin-wkwebview-
engine
。插件背后的想法如下:

1. 创建一个定义各种WebRTC类和函数的Javascript文件(WebRTC.js),并将调用传递给WKWebView,例如:

(function() {  if (!window.navigator) window.navigator = {};  window.navigator.getUserMedia = function() {    webkit.messageHandlers.callbackHandler.postMessage(arguments);  }})();

2. 在WKWebView中,在文档开始处插入脚本:

let contentController = WKUserContentController();contentController.add(self, name: "callbackHandler")let script = try! String(contentsOf: Bundle.main.url(forResource: "WebRTC", withExtension: "js")!, encoding: String.Encoding.utf8)contentController.addUserscript(WKUserscript(source: script, injectionTime: WKUserscriptInjectionTime.atdocumentStart, forMainframeOnly: true))let config = WKWebViewConfiguration()config.userContentController = contentControllerwebView = WKWebView(frame: CGRect.zero, configuration: config)

3. 收听从Javascript发送的消息:

class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate, WKscriptMessageHandler {  var webView: WKWebView!  func userContentController(_ userContentController: WKUserContentController, didReceive message: WKscriptMessage) {    if message.name == "callbackHandler" {      print(message.body)      // make native calls to the WebRTC framework here    }  }}

4. 如果需要在Javascript领域中执行成功或失败回调,请直接在WKWebView中评估函数调用:

webView.evaluateJavascript("callback({id: (id), status: 'success', args: ...})", completionHandler: nil)

这些回调需要 调用 之前
存储在Javascript中的哈希中

postMessage
,然后必须将哈希键发送到WKWebView。这是
commandId
插件中的。

int exec_id = 0;function exec(success, failure, ...) {  // store the callbacks for later  if (typeof success == 'function' || typeof failure == 'function') {    exec_id++;    exec_callbacks[exec_id] = { success: success, failure: failure };    var commandId = exec_id;  }  webkit.messageHandlers.callbackHandler.postMessage({id: commandId, args: ...})}// the native pre calls this directly with the same commandId, so the callbacks can be performed and releasedfunction callback(opts) {  if (opts.status == "success") {    if (typeof exec_callbacks[opts.id].success == 'function') exec_callbacks[opts.id].success(opts.args);  } else {    if (typeof exec_callbacks[opts.id].failure == 'function') exec_callbacks[opts.id].failure(opts.args);  }  // some WebRTC functions invoke the callbacks multiple times  // the native Cordova plugin uses setKeepCallbackAs(true)  if (!opts.keepalive) delete exec_callbacks[opts.id];}

5.
当然,为您的项目添加

NSCameraUsageDescription
NSMicrophoneUsageDescription
权限
Info.plist

Keep in mind this is a non-trivial task, but that’s the general idea behind
bridging Javascript, WKWebView, and native framework pre with asynchronous
callbacks.



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/364263.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号