ios 如何清除 UIWebView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6576154/
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
How To Clear A UIWebView
提问by Tom Cuzz
I am very new to the whole programming business, and was wondering if there is any way to clear the contents of a UIWebView
in iphone programming, so that the loading symbol for the next view is not showing up in front of the last view.
Many Thanks,
Thomas
我对整个编程业务很陌生,想知道是否有什么方法可以清除UIWebView
iphone编程中a的内容,这样下一个视图的加载符号就不会出现在上一个视图的前面。非常感谢,托马斯
回答by Constantino Tsarouhas
Try setting the URL to about:blank
and reload the page.
尝试将 URL 设置为about:blank
并重新加载页面。
回答by 2cupsOfTech
Just load an empty html string into it
只需将一个空的 html 字符串加载到其中
[self.webView loadHTMLString:@"" baseURL:nil];
[self.webView loadHTMLString:@"" baseURL:nil];
回答by Bill Patterson
Answer extension for documentation purposes to maybe help someone else:
出于文档目的的回答扩展可能会帮助其他人:
I had the same desire (clear content before loading next url) but had a UIWebView delegate set to receive webviewDidFinishLoad:(UIWebView)webview
message and update another part of UI in response.
我有同样的愿望(在加载下一个 url 之前清除内容)但是有一个 UIWebView 委托设置为接收webviewDidFinishLoad:(UIWebView)webview
消息并更新 UI 的另一部分作为响应。
Problem: the call to clear the content to alsocalled delegate method, so getting false-hits (that is, getting call when clear is done, too, but delegate is coded to expect call only when real content is loaded).
问题:清除内容的调用也调用了委托方法,因此会出现错误命中(即,清除完成后也调用,但委托被编码为仅在加载实际内容时才期望调用)。
Solution: use a known URL for clear, and have webviewDidFinishLoad:
ignore calls made when that URL is finished:
解决方案:使用已知 URL 进行清除,并webviewDidFinishLoad:
忽略该 URL 完成后的调用:
- (void) startLoadOfNextURL:(NSURL*)url
{
// clear:
[self.webView loadHTMLString:@"" baseURL:nil];
// Load real next URL
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(@"WebView finished loading: %@", webView);
if ([self.webView.request.URL.absoluteString isEqualToString:@"about:blank"]) {
NSLog(@" This is Blank. Ignoring as false event.");
}
else {
NSLog(@" This is a real url");
[self updateUIInSomeWay];
}
}
Note: using this:
注意:使用这个:
[self.webView loadHTMLString:@"about:blank" baseURL:nil];
actually causes the words "about:blank" to appear as text in the webview's content pane!
实际上会导致“about:blank”这个词在 webview 的内容窗格中显示为文本!
Final complication: In practice, with my two [webview load...]
calls so close together, I was finding that instead of a "loaded" event for the clear, the webview was actually canceling it in favor of the second request and calling webView: didFailLoadWithError:
for the first load request. Thus, I had to put similar code in that event:
最后的复杂性:在实践中,由于我的两个[webview load...]
调用如此接近,我发现 webview 实际上取消了它而不是一个“加载”事件以支持第二个请求并调用webView: didFailLoadWithError:
第一个加载请求。因此,我不得不在该事件中放置类似的代码:
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(@"WebView error on: %@", webView);
NSLog(@"Error is: %@", error);
NSURL* failingURL = [error.userInfo objectForKey:@"NSErrorFailingURLKey"];
if ([failingURL.absoluteString isEqualToString:@"about:blank"]) {
NSLog(@" This is Blank. Ignoring.");
}
else {
NSLog(@" This is a real URL.");
[self doSomethingAboutError];
}
}
回答by neoneye
Swift, Xcode 7 beta 5
斯威夫特,Xcode 7 测试版 5
webView.loadRequest(NSURLRequest(URL: NSURL(string: "about:blank")!))
回答by ViJay Avhad
Swift 4.0 , XCODE 9
斯威夫特 4.0 , Xcode 9
webView.loadRequest(URLRequest.init(url: URL.init(string: "about:blank")!))
回答by AbdulRehman Warraich
Same answer in Swift 4.2, xCode 10
Swift 4.2 中的相同答案,xCode 10
if let clearURL = URL(string: "about:blank") {
myWebView.loadRequest(URLRequest(url: clearURL))
}