我正在制作一个通过 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 中是否没有这种方法。