wkwebview不会在iframe

时间:2016-02-05 05:33:44

标签: ios youtube wkwebview

我正在尝试在iOS 9下使用WKWebView,其中一个页面碰巧在iframe中有一个youtube视频。视频无法显示。同样的视频在Safari中工作,所以似乎不是iOS问题,我假设我在视图中缺少配置选项。示例网址为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)")
    }
}

1 个答案:

答案 0 :(得分:1)

转到Projects info.plist文件 添加了一个名为NSAppTransportSecurity的密钥作为字典。 添加了一个名为NSAllowsArbitraryLoads的子键作为布尔值,并将其值设置为YES。它有效。enter image description here