views:

156

answers:

2

I am looking to find out if a web page has changed, I was going to use the content length of the web page but have not seen a way to do so. Any ideas? Or can anyone think of another way to check periodically if a web page has changed?

Any ideas are appreciated.

Thanks, Chris

A: 

If you mean with changed wether navigation has occured, you could use a custom UIWebViewDelegate and set a flag when e.g. -(void)webViewDidFinishLoad: occured.

You might want to check UIWebViews property request to check wether the URL actually differs.

If you want to check wether the content has changed you could retrieve it e.g. like this:

NSString* script  = @"document.body.innerHTML";
NSString* content = [webView stringByEvaluatingJavaScriptFromString:script];

Or retrieve the length e.g. like this:

NSString* script = @"document.body.innerHTML.length";
int length = [[webView stringByEvaluatingJavaScriptFromString:script] integerValue];
Georg Fritzsche
A: 

I am assuming you want to know if the web page you are already showing is different than the web page you would get if you hit the server again.

You cannot do much with the documented interfaces in UIWebView.

You can use an NSURLConnection to ask for just the headers of a web page and not the actual content. Once you get the headers, look at fields like "Last-Modified" and "Content-Length" to see if it has changed. You can also look into the 304 not modified response code.

Set the HTTPMethod of a new NSURLRequest to HEAD instead of GET to not get the body. Set your class as the delegate of an NSURLConnection created with that request. Handle the following delegate callback and examine headers in the response.

-(void) connection:(NSURLConnection *)inConnection didReceiveResponse:(NSURLResponse *)inResponse;

For more information look here:

http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol

drawnonward