Swift 屏幕熄灭时使用 WebRTC 的 WKWebView 退出广播
WKWebView using WebRTC quit broadcasting when screen goes off in Swift
我正在 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)
}
}
以下是警报处理 window 和避免重复重新加载。
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 支持的不同背景模式。您可以出于某些原因要求让麦克风保持开启状态。
Record voice in background(when i tapped home button) using AVAudioRecorder
我正在 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)
}
}
以下是警报处理 window 和避免重复重新加载。
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 支持的不同背景模式。您可以出于某些原因要求让麦克风保持开启状态。 Record voice in background(when i tapped home button) using AVAudioRecorder