在UIWebview中,我想更改
<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(…)
到目前为止没有成功。
最佳答案
在这里,我做了大致的演示代码来解决您的问题。提出您的逻辑,它将解决您的问题。
它已经过测试,看起来运行良好。
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)
}
}
关于ios - 如何更改UIWebView中的src值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46848829/