简体   繁体   English

从 WKWebView 访问设备摄像头无法启动

[英]Access device camera from WKWebView doesn't start

I know that this question was asked many times but after hours of testing I'm still for something which works.. I try to load an url from a WKWebView in Swift.我知道这个问题被问了很多次,但是经过数小时的测试,我仍然在寻找可行的方法。我尝试从 Swift 中的 WKWebView 加载 url。 That's the easy part.那是容易的部分。 In this pointed website the user can start automatically (if permission granted) the camera feed.在这个指向的网站中,用户可以自动启动(如果获得许可)摄像头馈送。 This works fine from a computer with camera.这适用于带摄像头的计算机。

With this code below I try to achieve the same goal but natively with an iOS App in Swift.使用下面的代码,我尝试使用 Swift 中的 iOS 应用程序来实现相同的目标。 The webview is loaded, I prompted for the camera permission because I filled the Infos.plist but the camera feed never starts.. If someone has an idea? webview 已加载,我提示摄像头权限,因为我填写了 Infos.plist 但摄像头馈送从未启动。如果有人有想法? Thanks!谢谢!

import UIKit
import WebKit
import AVFoundation

class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
    
    var webView: WKWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        AVCaptureDevice.requestAccess(for: AVMediaType.video) { response in
            if response {
                //access granted
                DispatchQueue.main.async {
                    self.webView = WKWebView(frame: .zero)

                    self.view.addSubview(self.webView)

                    let layoutGuide = self.view.safeAreaLayoutGuide

                    self.webView.translatesAutoresizingMaskIntoConstraints = false
                    self.webView.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
                    self.webView.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true
                    self.webView.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true
                    self.webView.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor).isActive = true

                    if let url = URL(string: "https://webrtc.github.io/samples/src/content/devices/input-output/") {
                        self.webView.load(URLRequest(url: url))
                        self.webView.load(URLRequest(url: url, cachePolicy: .reloadRevalidatingCacheData, timeoutInterval: 15))
                    }
                }
            }
        }
    }
}

After hours of testing I finally found a working solution.经过数小时的测试,我终于找到了一个可行的解决方案。 A WKWebViewConfiguration was needed AND a javascript code called as WKUserScript .需要WKWebViewConfiguration和称为WKUserScript的 javascript 代码。 Here is where I find the solution: https://developer.apple.com/forums/thread/134216这是我找到解决方案的地方: https://developer.apple.com/forums/thread/134216

Swift code: Swift 代码:

import UIKit
import WebKit

class ViewController: UIViewController {
    
    var webView: WKWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
            
        let userScriptURL = Bundle.main.url(forResource: "UserScript", withExtension: "js")!
        let userScriptCode = try! String(contentsOf: userScriptURL)
        let userScript = WKUserScript(source: userScriptCode, injectionTime: .atDocumentStart, forMainFrameOnly: false)
        
        let webConfiguration = WKWebViewConfiguration()
            webConfiguration.mediaTypesRequiringUserActionForPlayback = []
            webConfiguration.allowsInlineMediaPlayback = true
            webConfiguration.userContentController.addUserScript(userScript)
        
        self.webView = WKWebView(frame: .zero, configuration: webConfiguration)

        self.view.addSubview(self.webView)

        let layoutGuide = self.view.safeAreaLayoutGuide

        self.webView.translatesAutoresizingMaskIntoConstraints = false
        self.webView.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
        self.webView.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true
        self.webView.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true
        self.webView.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor).isActive = true

        if let url = URL(string: "https://webrtc.github.io/samples/src/content/getusermedia/gum/") {
            self.webView.load(URLRequest(url: url))
        }
    }
}

UserScript.js:用户脚本.js:

//injects this into webpage to listen to the camera request

(function() {

  if (!window.navigator) window.navigator = {};

    //if getuserMedia is called -> If the user requests native camera

        window.navigator.getUserMedia = function() {

            webkit.messageHandlers.callbackHandler.postMessage(arguments);

  }

})();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在WKWebView中加载本地文件在设备中不起作用 - Loading local file in WKWebView doesn't working in device 无法访问 WKWebView 中的麦克风和摄像头 - Unable to access Microphone and Camera in WKWebView 在 wkwebview 中启用相机和麦克风访问 - Enable Camera and Mic access in wkwebview 是否可以在 iOS 上的 WKWebView 中访问相机? - Is it possible to access camera in WKWebView on iOS? UIImagePickerController出现但摄像头无法在iOS 5上启动 - UIImagePickerController appears but camera doesn't start on iOS 5 如果用户拒绝访问摄像头,如何防止WKWebView显示摄像头模式? - How do I prevent a WKWebView from presenting the Camera modal if a user has denied access to the camera? WKWebView 从文档目录加载 html 在设备上不起作用 - WKWebView load html from document directory don't work on device WKWebView 不允许在应用程序中访问相机 - WKWebView does not allow camera access in application 我的应用当前不是从主页开始,而是直接转到AR摄像头 - my app currently doesn't start from the main page and instead goes straight to the AR camera WKWebview javascript可在模拟器上运行,但如果视图不在屏幕上,则无法在设备上运行 - WKWebview javascript works on simulator but doesn't work on device if the view isn't on the screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM