Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 如何使webview只加载特定的网站,如youtube?_Html_Swift_Webview_Youtube - Fatal编程技术网

Html 如何使webview只加载特定的网站,如youtube?

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;}<

我是Swift的新手,我正在用webview制作一个可以显示youtube视频的应用程序。我的问题是我不想让我的用户转到另一个url或类似的东西。我想让我的应用程序加载youtube.com。我使用HTML而不是URL,因为我可以告诉我的webview它的高度和宽度

HTML代码:

<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
}