Stop Images from loading in UIWebView

↘锁芯ラ 提交于 2019-11-29 11:35:27

I have 3 thoughts.

  1. Use a proxy to filter the HTML
  2. Filter the HTML yourself by downloading it first
  3. Just download the html and load it using a file:// URL. This will most likely prevent the image references from being resolved - though it will leave ugly squares everywhere
yonel

The UIWebView delegate approach does not work, you're right ! new answer:

Right, you need to go one level deeper to catch the NSURLRequest coming from the UIWebView. The blog you're referring to makes use of the NSURLCache and I think this is a good start point:

Did you try to subclass the NSURLCache, and then override the -(NSURLCacheResponse*) cachedResponseForRequest:(NSURLRequest*) request

If you want to avoid the request being performed, you need to return something (as an NSURLCachedResponse). You can for instance, get an image that is statically defined in your app (maybe an PNG with size 0,0 ).

If you return nil, the request will be performed.

I'm using this approach to force the UIWebView to get populated with a local cache. See my detailled answer about this here :

How to save the content in UIWebView for faster loading on next launch?


Wrong first answer :/

Add yourself as delegate to the UIWebView (UIWebViewDelegate).

For each image that the UIWebView attempts to load, the delagate gets the following callback invoked :

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

Just check if the request is related to an image you want to avoid to download. In such a case, just return NO to this callback : the image won't be loaded in the UIWebView.

By using this approach, no change is required on server side.

I would suggest another option to try:

using webView:shouldStartLoadWithRequest:navigationType: of the WebView's delegate to return NO when it try to load image.

You can use the Following line of code, Whenever you want to stop the webView loading.

[webView stopLoading];

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!