views:

472

answers:

2

I have a website that I wish to load in a UIWebView, but it is full of images and takes ages to load. The images are useless, and only serve to reduce the usability on the iPhone. I dont own the website so I cannot change the site's actual code.

The webpage is heavily linked in to the web with ASP.NET and AJAX (needs external files), so i dont think it is possible to have it load an HTML string.

I want to stop the images from loading altogether. So how do i block them? Change the HTML code as its loading somehow, or block images from being loaded?

+2  A: 

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
sylvanaar
The problem is, its a series of dynamically loaded pages, that have content based on what has been inputted on previous pages. So downloading them and linking them is not an option.
Zac Altman
Then I would use a filtering proxy - its very similar to using an ad blocker on your web browser
sylvanaar
how would I go about implementing that?
Zac Altman
http://www.icab.de/blog/2009/08/18/url-filtering-with-uiwebview-on-the-iphone/ - so I found that, but I am completely phased by it and cannot get it to work...any help?
Zac Altman
+1  A: 

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 :

http://stackoverflow.com/questions/1343515/how-to-save-the-content-in-uiwebview-for-faster-loading-on-next-launch/2468722#2468722


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.

yonel
I tried that, but it only calls that when the page itself is being loaded. None of the page elements go through there (images, JS, CSS...)
Zac Altman
you're right, I've described another approach in the answer. Cheers.
yonel