How to get camera and microphone access in WKWebView cocoa?

纵然是瞬间 提交于 2019-11-28 13:06:18

问题


In my cocoa application I am using WKWebView and everything works fine, but when I try to make an Audio or Video call, it is not happening, as I don't have the permissions for camera and microphone. So, how can I get access to both. I've injected javascript is there any way so that we can allow Microphone and camera by default using script?

i've tried allow(tried to resolve the promise) using below code, but still i am unable to access microphone. As this popup will not show in WKWebView so how do i take permission.

  async function wrapperFunc() {
    try {
        alert("resolved 1");
        let r1 = await someFunc();
        let r2 = await someFunc2(r1);
        // now process r2
        return someValue;     // this will be resolved value of the returned promise
    } catch(e) {
        throw e;      // let caller know the promise rejected with this reason
    }
}

wrapperFunc().then(result => {
                   // got final result
                   alert("resolved");
                   }).catch(err => {
                            // got error
                            });

Any suggestions?

Thanks in Advance !!


回答1:


Here is the steps you need to follow :

  • First add the NSCameraUsageDescription and NSMicrophoneUsageDescription permissions to the Info.plist for your project

  • Now Add JS file (WebRTC.js) that defines various WebRTC classes, functions & passes the calls to the WKWebView.

Let say for ex:

(function() {
  if (!window.navigator) window.navigator = {};
  window.navigator.getUserMedia = function() {
    webkit.messageHandlers.callbackHandler.postMessage(arguments);
  }
})();
  • In the WKWebView inject the script at the document 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 = contentController

webView = WKWebView(frame: CGRect.zero, configuration: config)

  • Now Add this :

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
    }
} }
  • If success or failure callbacks need to be performed back in JavaScript-land, evaluate the function call directly within the WKWebView:

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

These callbacks need to be stored in a hash in the JavaScript before calling postMessage, then the hash key must be sent to the WKWebView. This is the commandId in the plugins.

int exec_id = 0;
function exec(success, failure, ...) {
  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: ...})
}

function 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);
  }
  if (!opts.keepalive) delete exec_callbacks[opts.id];
}

Hope this helps.



来源:https://stackoverflow.com/questions/53513521/how-to-get-camera-and-microphone-access-in-wkwebview-cocoa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!