wkwebview 不会在 iframe 中显示 youtube 视频
wkwebview won't display a youtube video in an iframe
我正在尝试在 iOS 9 下使用 WKWebView,其中一个页面恰好在 iframe 中有一个 youtube 视频。视频不显示。同一视频在 Safari 中有效,因此似乎不是 iOS 问题,我想我在视图中缺少配置选项。样本 URL 是 http://www.w3schools.com/html/tryit.asp?filename=tryhtml_youtubeiframe.
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate
{
var webView : WKWebView!
override func viewDidLoad()
{
super.viewDidLoad()
let webviewConfiguration = WKWebViewConfiguration()
webviewConfiguration.allowsInlineMediaPlayback = true
webviewConfiguration.preferences.javaScriptCanOpenWindowsAutomatically = true
webviewConfiguration.allowsAirPlayForMediaPlayback = true
webviewConfiguration.allowsPictureInPictureMediaPlayback = true
webviewConfiguration.requiresUserActionForMediaPlayback = false
webView = WKWebView(frame: self.view.bounds,
configuration: webviewConfiguration)
webView.navigationDelegate = self
self.view = webView
let url = NSURL(string: "http://www.w3schools.com/html/tryit.asp?filename=tryhtml_youtubeiframe")
let req = NSURLRequest(URL: url!)
self.webView!.loadRequest(req)
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
func webView(webView: WKWebView,
didFailNavigation navigation: WKNavigation!,
withError error: NSError)
{
print("didFailNavigation \(error)")
}
func webView(webView: WKWebView,
didFailProvisionalNavigation navigation: WKNavigation!,
withError error: NSError)
{
print("didFailProvisionalNavigation \(error)")
}
}
转到项目 info.plist 文件
添加了一个名为 NSAppTransportSecurity 的键作为字典。
添加了一个名为 NSAllowsArbitraryLoads 的子键作为布尔值并将其值设置为 YES。有效。
我正在尝试在 iOS 9 下使用 WKWebView,其中一个页面恰好在 iframe 中有一个 youtube 视频。视频不显示。同一视频在 Safari 中有效,因此似乎不是 iOS 问题,我想我在视图中缺少配置选项。样本 URL 是 http://www.w3schools.com/html/tryit.asp?filename=tryhtml_youtubeiframe.
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate
{
var webView : WKWebView!
override func viewDidLoad()
{
super.viewDidLoad()
let webviewConfiguration = WKWebViewConfiguration()
webviewConfiguration.allowsInlineMediaPlayback = true
webviewConfiguration.preferences.javaScriptCanOpenWindowsAutomatically = true
webviewConfiguration.allowsAirPlayForMediaPlayback = true
webviewConfiguration.allowsPictureInPictureMediaPlayback = true
webviewConfiguration.requiresUserActionForMediaPlayback = false
webView = WKWebView(frame: self.view.bounds,
configuration: webviewConfiguration)
webView.navigationDelegate = self
self.view = webView
let url = NSURL(string: "http://www.w3schools.com/html/tryit.asp?filename=tryhtml_youtubeiframe")
let req = NSURLRequest(URL: url!)
self.webView!.loadRequest(req)
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
func webView(webView: WKWebView,
didFailNavigation navigation: WKNavigation!,
withError error: NSError)
{
print("didFailNavigation \(error)")
}
func webView(webView: WKWebView,
didFailProvisionalNavigation navigation: WKNavigation!,
withError error: NSError)
{
print("didFailProvisionalNavigation \(error)")
}
}
转到项目 info.plist 文件
添加了一个名为 NSAppTransportSecurity 的键作为字典。
添加了一个名为 NSAllowsArbitraryLoads 的子键作为布尔值并将其值设置为 YES。有效。