views:

1610

answers:

5

How can I use the UIWebView in Xcode so that when it loads up pages it DOESN'T download the images (to cause a faster loading page)?

A: 

Be the delegate for the UIWebView, then intercept the call:

– webView:shouldStartLoadWithRequest:navigationType:

Check the values of navigationType in the documentation. I believe you'll be best served by returning NO on navigationType == UIWebViewNavigationTypeOther.

Marco
webView:shouldStartLoadWithRequest:navigationType: gets called after the download has occurred.
Harry
+2  A: 

UIWebView is a pale, poor little shadow of WebKit's full WebView, for which this is easy. -webView:shouldStartLoadWithRequest:navigationType: only gets called for navigation. It doesn't get called for every request like WebPolicyDelegate does on mac. With UIWebView, here's how I would attack this problem:

Implement -webView:shouldStartLoadWithRequest:navigationType: and set it to always return NO. But you'll also take the request and spawn an NSURLConnection. When the NSURLConnection finishes fetching the data, you're going to look through it for any IMG tags and modify them to whatever placeholder you want. Then you will load the resulting string into the UIWebView using -loadHTMLString:baseURL:.

Of course parsing the HTML is not a trivial task on iPhone, and Javascript loaders are going to give you trouble, so this isn't a perfect answer, but it's the best I know of.

Rob Napier
A: 

I've been looking for ages for this information. Thanks Rob and Marco

Sotiris
A: 

does this actually cause the page to load faster? it sounds like the images are still being downloaded, but we're just not feeding them to the UIWebView.

or does shouldStartLoadWithRequest just load the HTML text first?

mike
A: 

expanding on Rob's answer. I noticed that when loadHTMLString:baseURL: and always returning NO, that webView:shouldStartLoadWithRequest:navigationType: just keeps getting called. (i suspect loadHTMLString invokes another shouldStartLoadWithRequest).

so what I had to do was alternate between returning YES/NO and I used NSScanner to parse the HTML and change src="http://..." to src=""

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (pageHasNoImages==YES)
    {
     pageHasNoImages=FALSE;
     return YES;  
    }
    NSString* newHtml;
    NSString* oldHtml;
    NSData *urlData;
    NSURLResponse *response;
    NSError *error;
    urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    oldHtml=[[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];

    newHtml=[self.detail scannerReplaceImg:oldHtml]; // my own function to parse HTML
    newHtml=[self.detail scannerReplaceAds:newHtml]; // my own function to parse HTML
    if (newHtml==nil) 
    {
     NSLog(@"newHtml is nil");
     newHtml=oldHtml;
    }
    [oldHtml release];

    pageHasNoImages=TRUE;
    [web loadHTMLString:newHtml baseURL:request.URL];

    return NO;
}
mike