0

我正在制作一个通过 Swift 中的 WKWebVew 进行语音聊天的应用程序。

即使屏幕关闭,其他用户也应该能听到您说话的声音。

但是,如果在使用 WKWebView 时关闭设备屏幕,则广播结束并刷新 WKWebView。

import UIKit
import WebKit

class WebViewController: UIViewController {
    @IBOutlet var webView: WKWebView!

    override func loadView() {
        super.loadView()

        let webConfiguration = WKWebViewConfiguration()
        webConfiguration.ignoresViewportScaleLimits = true
        webConfiguration.allowsInlineMediaPlayback = true
        webConfiguration.allowsAirPlayForMediaPlayback = true
        webConfiguration.allowsPictureInPictureMediaPlayback = true
        webConfiguration.mediaTypesRequiringUserActionForPlayback = []

        webView = WKWebView(frame: self.view.bounds, configuration: webConfiguration)
        webView.uiDelegate = self
        webView.navigationDelegate = self

        self.view = self.webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        webView.scrollView.bounces = false

        let localFilePath = Bundle.main.url(forResource: "www/test", withExtension: "html")
        let request = URLRequest(url: localFilePath!)
        webView.load(request)
    }
}

以下是警报窗口的处理和避免重复重新加载。

extension WebViewController: WKUIDelegate {
    //alert
    func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo,
                 completionHandler: @escaping () -> Void) {
        let alertController = UIAlertController(title: "", message: message, preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "OK", style: .default,
                                                handler: { (action) in completionHandler() }))
        self.present(alertController, animated: true, completion: nil)
    }

    //confirm
    func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo,
                 completionHandler: @escaping (Bool) -> Void) {

        let alertController = UIAlertController(title: "", message: message, preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in completionHandler(true) }))
        alertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in completionHandler(false) }))
        self.present(alertController, animated: true, completion: nil)
    }

    //prompt
    func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String,
                 defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) {
        let alertController = UIAlertController(title: "", message: prompt, preferredStyle: .alert)
        alertController.addTextField { (textField) in textField.text = defaultText }
        alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
            if let text = alertController.textFields?.first?.text {
                completionHandler(text)
            } else {
                completionHandler(defaultText)
            }
        }))

        alertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
            completionHandler(nil) }))

        self.present(alertController, animated: true, completion: nil)
    }

    // href="_blank"
    func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
        if navigationAction.targetFrame == nil {
            webView.load(navigationAction.request)
        }
        return nil
    }
}

extension WebViewController: WKNavigationDelegate {
    // prevent reload
    public func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
        webView.reload()
    }
}

据说由于视图层次结构,当 WKWebView 中的屏幕熄灭时,WKWebView 停止工作。

即使通过背景模式关闭屏幕,有没有办法修复视图?

在 Android 中,您可以通过 Foreground Service 来 Intent WebView,因此即使屏幕关闭,WebView 也能正常工作。我想知道 Swift 中是否没有这种方法。

4

1 回答 1

1

不,没有这样的方法。当屏幕关闭时。即设备被锁定,您将无法使用 WKWebView 的功能。即使您的应用程序处于后台模式。您将不得不找到另一种解决方案来保持麦克风打开并将其发送到远程服务器。了解 iOS 支持的不同背景模式。您可以出于某些原因要求打开麦克风。 使用 AVAudioRecorder 在后台录制语音(当我点击主页按钮时)

于 2021-07-21T12:54:59.863 回答