Wo ist die IOS -WKWebView -Kamera -Integration aktiviert?
Posted: 02 May 2025, 08:52
Ich habe den Code und die Optionen in den Antworten hier ausprobiert.
Code: Select all
import Foundation
import WebKit
class MyDelegate: NSObject, WKUIDelegate {
// Handle media capture permissions
func webView(_ webView: WKWebView,
requestMediaCapturePermissionFor origin: WKSecurityOrigin,
initiatedByFrame frame: WKFrameInfo,
type: WKMediaCaptureType,
decisionHandler: @escaping (WKPermissionDecision) -> Void) {
// Always grant permission for demonstration purposes
decisionHandler(.grant)
}
func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
print("JavaScript alert with message: \(message)")
completionHandler()
}
}
< /code>
Der ContentView Swift für Haupt -Swiftui: < /p>
import SwiftUI
import WebKit
struct WebView: UIViewRepresentable {
let htmlFileName: String
let delegate = MyDelegate()
func makeUIView(context: Context) -> WKWebView {
let webViewConfiguration = WKWebViewConfiguration();
webViewConfiguration.allowsInlineMediaPlayback = true;
let webView = WKWebView(frame:.zero, configuration:webViewConfiguration)
webView.configuration.preferences.javaScriptEnabled = true;
webView.uiDelegate = delegate
webView.isInspectable = true //////////////// DEBUGGING purpose
webView.configuration.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")
return webView
}
func updateUIView(_ webView: WKWebView, context: Context) {
if let url = Bundle.main.url(forResource: htmlFileName, withExtension: "html") {
webView.loadFileURL(url, allowingReadAccessTo: url)
}
}
}
struct ContentView: View {
var body: some View {
WebView(htmlFileName: "index") // Replace "index" with your HTML file name
.edgesIgnoringSafeArea(.all)
}
}