[英]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.