2017-10-20 92 views
0

在一个UIWebView我想改变如何更改UIWebView中的src值?

<iframe src=“//player.vimeo.com/video/164231311?autoplay=1” width=“700" height=“394” frameborder=“0" webkitallowfullscreen=“” mozallowfullscreen=“” allowfullscreen=“”></iframe> 

<iframe src=“//player.vimeo.com/video/164231311" width=“700” height=“394" frameborder=“0” webkitallowfullscreen=“” mozallowfullscreen=“” allowfullscreen=“”></iframe> 

,因为我希望用户有一个播放按钮,而不是因为自动播放暂停按钮呈现不允许在iOS。如下图所示,用户直接看到播放按钮而不是暂停按钮会更自然。

我该如何以简单的方式做到这一点?我试过一些东西,如

webView.stringByEvaluatingJavaScript(from:“document.getElementsByTagName(…) 

目前为止没有成功。

enter image description here

回答

1

这里我做了粗略的演示代码来解决您的问题。把你的逻辑与它,它会解决你的问题。它已经过测试,看起来完美无缺。

import UIKit 

class ViewController: UIViewController,UIWebViewDelegate { 

    @IBOutlet weak var webView: UIWebView! 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     webView.delegate = self 
     webView.loadRequest(URLRequest(url: URL(string: "file:///Users/user/Downloads/index.html")!)) 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func webViewDidFinishLoad(_ webView: UIWebView) { 
     // get your current iframe src value 
     let iframeSrcValue:String = webView.stringByEvaluatingJavaScript(from: "document.getElementsByTagName('iframe')[0].src")! 

     // Here is your current value with AutoPlay 
     print(iframeSrcValue) 

     //Create new URL without Auto Play 
     let modifiedSrcValue = "https://www.youtube.com/embed/td8pYyuCIIs" 

     // Apply it to webview 
     let evaluate: String = "document.getElementsByTagName('iframe')[0].src='\(modifiedSrcValue)';" 
     webView.stringByEvaluatingJavaScript(from: evaluate) 
    } 

} 
+0

非常感谢@ arunjos007!我用你的建议进行了一些修改,并且做得很好! – MickeDG