我有一个简单的
WkWebView
,嵌入了 YouTube iframe。
我获得了高品质
我只能得到低质量的
我尝试了
?quality=high
并将 width
和 height
设置为更大的值,但这似乎永远不会影响任何事情。
struct TestView: UIViewRepresentable {
let html = """
<meta name="viewport"
content="width=device-width, shrink-to-fit=YES">
<iframe
width="100%"
height="100%"
src="https://www.youtube.com/embed/_7vP9vsnYPc" />
"""
func makeUIView(context: Context) -> WKWebView {
let config = WKWebViewConfiguration()
config.allowsInlineMediaPlayback = true
let webView = WKWebView(frame: .zero, configuration: config)
webView.loadHTMLString(html, baseURL: nil)
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) { }
}
如何解决此问题,以便“Mac(专为 iPad 设计)”获得更高的质量?
并不理想,但我能想到的最佳解决方案是替换 userAgent。在“Mac(专为iPad设计)”中,使用iPad用户代理。使用 Mac 可以实现更高的质量,尽管它仍然没有显示在质量选择中。这也破坏了全屏显示的选项。
我正在尝试做得更有活力一点:
func makeUIView(context: Context) -> WKWebView {
let webView = WKWebView()
if ProcessInfo.processInfo.isiOSAppOnMac {
let userAgent = webView.value(forKey: "userAgent") as? String
let newUserAgent = createCustomMacOsUserAgent(userAgent)
webView.customUserAgent = newUserAgent // < set a Mac user agent
}
return webView
}
func createCustomMacOsUserAgent(_ userAgent: String?) -> String {
var osVersion = "17_5"
var webKitVersion = "605.1.15"
if let userAgent = userAgent {
if let range = userAgent.range(of: "AppleWebKit/") {
let webKitVersionStart = userAgent[range.upperBound...]
if let endRange = webKitVersionStart.range(of: " ") {
webKitVersion = String(webKitVersionStart[..<endRange.lowerBound])
}
}
if let range = userAgent.range(of: "CPU OS ") {
let osVersionStart = userAgent[range.upperBound...]
if let endRange = osVersionStart.range(of: " ") {
osVersion = String(osVersionStart[..<endRange.lowerBound]).replacingOccurrences(of: "_", with: ".")
print("osVersion", osVersion)
}
}
}
return "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/\(webKitVersion)"
+ " (KHTML, like Gecko) Version/\(osVersion) Safari/\(webKitVersion)"
}