如何在 UIWebView iOS 中阻止加载图像? [英] How to block Load image in UIWebView iOS?

查看:33
本文介绍了如何在 UIWebView iOS 中阻止加载图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 UIWebView 加载 url,但不加载网页中的图片.我希望 UIWebView 只显示网页中的纯文本.

I want to use UIWebView to load url, but don't load the image in the webpage.I want UIWebView just display the plain text in the webpage.

谢谢.

推荐答案

最好的办法是为 webView 设置一个委托并实现委托方法:

Your best bet is to set a delegate for the webView and implement the delegate method:

  • webView:shouldStartLoadWithRequest:navigationType:

根据页面的构造方式,您可以获得有关何时加载图像的通知,并返回 NO 以阻止 webView 加载该资源.

Depending on how the page is constructed, you could get a notification for when the image wants to load, and return NO to stop the webView from loading that resource.

如果您没有在 xib 中设置 Web 视图的委托,则在设置的某个时刻:

If you didn't set your web view's delegate in the xib, then at some point in setup:

self.webview.delegate = self;

然后,在您设置为委托的类中,类似:

Then, in the class you set as the delegate, something like:

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{

NSRange rangeOfPNG = [request.URL.absoluteString rangeOfString:@".png"];
NSRange rangeOfJPG = [request.URL.absoluteString rangeOfString:@".jpg"];

if (rangeOfPNG.location != NSNotFound || rangeOfJPG.location != NSNotFound)
    {
    return (NO);    // Tells the webview to skip loading this part of the page
    }

return (YES);       // Allows the webview to load everything else
}

所以任何扩展名为 .png 或 .jpg 的内容都会被跳过.

So anything with an extension of .png or .jpg gets skipped.

这在很大程度上取决于您正在加载的页面的结构.对于更复杂的页面,可能没有特定图像的回调.

This is highly dependent on the structure of the page you're loading. For more complex pages, there might not be a callback for a specific image.

如果您知道要加载的页面的结构,这将大有帮助.

It helps greatly if you know the structure of the page you're attempting to load.

您还可以使用 Safari 检查页面的代码以确定其中的内容.此外,在 shouldStartLoadWithRequest 委托方法的开头设置断点可以让您确切地看到哪些请求通过了,并且您可以使用它来硬编码您希望 webview 跳过加载的字符串.

You can also use Safari to examine the code for the page to determine what's in there. Also, setting a breakpoint at the start of the shouldStartLoadWithRequest delegate method will let you see exactly what requests come through, and you might be able to use that to hardcode strings that you want the webview to skip loading.

这篇关于如何在 UIWebView iOS 中阻止加载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆