ios - 当屏幕在 Swift 中关闭时,使用 WebRTC 的 WKWebView 退出广播
问题描述
我正在制作一个通过 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 中是否没有这种方法。
解决方案
不,没有这样的方法。当屏幕关闭时。即设备被锁定,您将无法使用 WKWebView 的功能。即使您的应用程序处于后台模式。您将不得不找到另一种解决方案来保持麦克风打开并将其发送到远程服务器。了解 iOS 支持的不同背景模式。您可以出于某些原因要求打开麦克风。 使用 AVAudioRecorder 在后台录制语音(当我点击主页按钮时)
推荐阅读
- apache - 检查.htaccess中的引用标头时如何检查完全匹配
- javascript - 创建我自己的组件 vuejs,有两个不同的部分
- swift - 如何快速检查某个窗口是否打开
- sql - 如何在 BigQuery 中返回混合/多个数据?
- c# - iText7 从 IFormFile 创建一个文档
- python - TypeError: update_graph_scatter() 接受 0 个位置参数,但给出了 1 个
- django - 在 django 模型字段中保存 get_absolute_url 的结果
- android-studio - 折射错误,我无法提取方法错误消息
- python - 扫描 ODE 函数 python 的参数
- vhdl - 分配中的宽度不匹配:VHDL