ios 如何在不加载空页面的情况下清理 UIWebView 的内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10909329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:38:59  来源:igfitidea点击:

How to clean the content of UIWebView without loading an empty page?

iphoneiosuiwebview

提问by hzxu

I need to clean the content of UIWebView(in order to reuse it), but I have some authentication handling code in its delegate methods, so I do not want to load an empty page like about:blank to clean it, as it will trigger my authentication handling code. So is there a way for doing this? (also, by reusing it, I need to put a spinner on top of the web view, and when it loads another page, I don't want the user see the previous loaded page content, so that's why I need to clean it)

我需要清理 UIWebView 的内容(为了重用它),但我在它的委托方法中有一些身份验证处理代码,所以我不想加载像 about:blank 这样的空页面来清理它,因为它会触发我的身份验证处理代码。那么有没有办法做到这一点?(另外,通过重用它,我需要在 web 视图顶部放置一个微调器,当它加载另一个页面时,我不希望用户看到以前加载的页面内容,所以这就是我需要清理它的原因)

Thanks!

谢谢!

回答by Nilesh Kikani

You can just use this line of code :

你可以用下面这行代码:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];

Hereyou will find whole demo

在这里你会找到整个演示

回答by myeyesareblind

this did the trick for me:

这对我有用:

 [webView stringByEvaluatingJavaScriptFromString:@"document.open();document.close()"];

回答by Prasad

Try this out. This worked for me

试试这个。这对我有用

[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML = \"\";"];

回答by turingtested

If your only problem is that the spinner disappears when you load a blank page, you can use this code in webViewDidFinishLoad:

如果您唯一的问题是加载空白页面时微调器消失,您可以在 webViewDidFinishLoad 中使用以下代码:

– (void)webViewDidFinishLoad:(UIWebView *)currentWebView {
    if (![currentWebView.request.URL.description isEqualToString: @"about:blank"]) {
        self.mySpinner.hidden = YES;
    }
}

...then the spinner will go on until your webview is actually done loading.

...然后微调器将继续运行,直到您的 webview 实际加载完毕。