WKWebView 视频不内联播放
WKWebView video does not play inline
因此根据 Apple 的文档:https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/1614793-allowsinlinemediaplayback
我应该能够使用 Swift 4 轻松地内联播放视频,但无论我做什么,它总是会在本机视频播放器中打开视频。
这是我的代码:
convenience init(style: UITableViewCell.CellStyle, reuseIdentifier: String?, url: String = "", title: String) {
self.init(style: style, reuseIdentifier: reuseIdentifier)
self.videoTitleLabel.text = title
self.urlToVideo = url
setUpUI()
setUpLayout()
webView.backgroundColor = .smalt
webView.translatesAutoresizingMaskIntoConstraints = false
webView.configuration.allowsInlineMediaPlayback = true
webView.configuration.preferences.javaScriptEnabled = true
webView.load(URLRequest(url: URL(string: self.urlToVideo + "?playsinline")! ))
}
我做错了什么?
通过查看人们报告的其他一些问题和 Apple 文档中的详细信息,我认为问题在于必须在创建 WKWebView 之前设置 WKWebViewConfiguration。
来自苹果文档:
Using the WKWebViewConfiguration class, you can determine how soon a webpage is rendered, how media playback is handled, the granularity of items that the user can select, and many other options.
WKWebViewConfiguration is only used when a web view is first initialized. You cannot use this class to change the web view's configuration after it has been created.
这与 Apple 提供的使用 WKWebView (https://developer.apple.com/documentation/webkit/wkwebview) 的示例一致,您可以在其中看到 WKWebViewConfiguration 被传递到 WKWebView 设置。我已经在 playsinline 中添加了你的例子。
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsInlineMediaPlayback = true //** Added as an example for your case
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string:"https://www.apple.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}}
如果随后无法设置 属性 以避免这种混淆,这似乎更有意义,但从文档中可以看出它是这样工作的。
因此根据 Apple 的文档:https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/1614793-allowsinlinemediaplayback
我应该能够使用 Swift 4 轻松地内联播放视频,但无论我做什么,它总是会在本机视频播放器中打开视频。
这是我的代码:
convenience init(style: UITableViewCell.CellStyle, reuseIdentifier: String?, url: String = "", title: String) {
self.init(style: style, reuseIdentifier: reuseIdentifier)
self.videoTitleLabel.text = title
self.urlToVideo = url
setUpUI()
setUpLayout()
webView.backgroundColor = .smalt
webView.translatesAutoresizingMaskIntoConstraints = false
webView.configuration.allowsInlineMediaPlayback = true
webView.configuration.preferences.javaScriptEnabled = true
webView.load(URLRequest(url: URL(string: self.urlToVideo + "?playsinline")! ))
}
我做错了什么?
通过查看人们报告的其他一些问题和 Apple 文档中的详细信息,我认为问题在于必须在创建 WKWebView 之前设置 WKWebViewConfiguration。
来自苹果文档:
Using the WKWebViewConfiguration class, you can determine how soon a webpage is rendered, how media playback is handled, the granularity of items that the user can select, and many other options.
WKWebViewConfiguration is only used when a web view is first initialized. You cannot use this class to change the web view's configuration after it has been created.
这与 Apple 提供的使用 WKWebView (https://developer.apple.com/documentation/webkit/wkwebview) 的示例一致,您可以在其中看到 WKWebViewConfiguration 被传递到 WKWebView 设置。我已经在 playsinline 中添加了你的例子。
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsInlineMediaPlayback = true //** Added as an example for your case
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string:"https://www.apple.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}}
如果随后无法设置 属性 以避免这种混淆,这似乎更有意义,但从文档中可以看出它是这样工作的。