Html 如何使webview只加载特定的网站,如youtube?
我是Swift的新手,我正在用webview制作一个可以显示youtube视频的应用程序。我的问题是我不想让我的用户转到另一个url或类似的东西。我想让我的应用程序加载youtube.com。我使用HTML而不是URL,因为我可以告诉我的webview它的高度和宽度 HTML代码:Html 如何使webview只加载特定的网站,如youtube?,html,swift,webview,youtube,Html,Swift,Webview,Youtube,我是Swift的新手,我正在用webview制作一个可以显示youtube视频的应用程序。我的问题是我不想让我的用户转到另一个url或类似的东西。我想让我的应用程序加载youtube.com。我使用HTML而不是URL,因为我可以告诉我的webview它的高度和宽度 HTML代码: <html><head><style type=\"text/css\">body {background-color: transparent;color: white;}<
<html><head><style type=\"text/css\">body {background-color: transparent;color: white;}</style></head><body style=\"margin:0\"><iframe frameBorder=\"0\" height=\"" + String(describing: height) + "\" width=\"" + String(describing: width) + "\" src=\"http://www.youtube.com/embed/" + vid.videoId + "?showinfo=0&modestbranding=1&frameborder=0&rel=0\"></iframe></body></html>
body{背景色:透明;颜色:白色;}
您可以尝试使用UIWebViewDelegate
extension UIViewController: UIWebViewDelegate {
public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let url = request.url?.absoluteString ?? ""
print("WebView shouldStartLoadWithRequest url -> \(url)")
if !url.contains("youtube.com") {
// avoid loading strange urls..
// manage error as you need..
webView.stopLoading()
return false
}
return true
}
}
记住在viewdiload
方法中将UIViewController
设置为UIWebView
委托:
override func viewDidLoad() {
super.viewDidLoad()
myWebView.delegate = self
}
您可以尝试使用
UIWebViewDelegate
extension UIViewController: UIWebViewDelegate {
public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let url = request.url?.absoluteString ?? ""
print("WebView shouldStartLoadWithRequest url -> \(url)")
if !url.contains("youtube.com") {
// avoid loading strange urls..
// manage error as you need..
webView.stopLoading()
return false
}
return true
}
}
记住在viewdiload
方法中将UIViewController
设置为UIWebView
委托:
override func viewDidLoad() {
super.viewDidLoad()
myWebView.delegate = self
}