2016-02-05 70 views
0

顯示YouTube視頻,我試圖用一個WKWebView的iOS 9頁的一個恰好有一個iframe一個YouTube視頻下方。視頻不顯示。相同的視頻在Safari中工作,因此似乎不是iOS問題,我假設我缺少視圖中的配置選項。示例網址是http://www.w3schools.com/html/tryit.asp?filename=tryhtml_youtubeiframewkwebview不會在iframe

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

轉到項目Info.plist文件 增加了一個名爲NSAppTransportSecurity作爲字典的關鍵。 添加了一個名爲NSAllowsArbitraryLoads的子項,並將其值設置爲YES。有用。 enter image description here

+0

@TofuBeer嘗試...... –

+0

D'哦!謝謝。我爲服務器添加了異常,但並不知道,當然,youtube也是http,需要一個例外......過了漫長的一天:-)如果蘋果在這種情況下吐出一個錯誤,那就好了! – TofuBeer