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

ios – 在wkwebview中启用摄像头和麦克风访问,第1张

概述我有一个移动优化的网络应用程序,利用getUserMedia访问网络摄像头麦克风资源. 我将此应用程序包装在WKWebView中,因为我想提供本机应用程序体验.我知道iOS不允许通过浏览器访问摄像头 – 但是有没有办法获得使用本机代码(与包装器一起)的网络摄像头/麦克风的权限并将其提供给Web应用程序 – 也许通过某种方式指向getUserMedia本地流源? 是的,请看一下 cordova-p 我有一个移动优化的网络应用程序,利用getUserMedia访问网络摄像头和麦克风资源.

我将此应用程序包装在WKWebVIEw中,因为我想提供本机应用程序体验.我知道iOS不允许通过浏览器访问摄像头 – 但是有没有办法获得使用本机代码(与包装器一起)的网络摄像头/麦克风的权限并将其提供给Web应用程序 – 也许通过某种方式指向getUserMedia本地流源?

解决方法 是的,请看一下 cordova-plugin-iosrtc和 cordova-plugin-wkwebview-engine.插件背后的想法如下:

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

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

2.在WKWebVIEw中,在文档start处注入脚本:

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-land中执行成功或失败回调,请直接在WKWebVIEw中评估函数调用:

webVIEw.evaluateJavaScript("callback({ID: \(ID),status: 'success',args: ...})",completionHandler: nil)

在调用postMessage之前,这些回调需要存储在JavaScript的哈希中,然后必须将哈希键发送到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 code 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中.

请记住,这是一项非常重要的任务,但这是使用异步回调桥接JavaScript,WKWebVIEw和本机框架代码的总体思路.

总结

以上是内存溢出为你收集整理的ios – 在wkwebview中启用摄像头和麦克风访问全部内容,希望文章能够帮你解决ios – 在wkwebview中启用摄像头和麦克风访问所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1209562.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-04
下一篇2022-06-04

发表评论

登录后才能评论

评论列表(0条)

    保存