views:

13

answers:

1

I want to check the dates on files -- zip, jpg, or whatever -- on my server and, if they are newer than the copies on my iPhone, to download them.

I wrote the following method based on a post here that's about a year old. It has two problems:

+ (NSString *) f_GetServerFileDate:(NSString *)MyURL {
    NSURL *oURL = [NSURL URLWithString:MyURL];
    NSURLRequest *oRequest = [NSURLRequest requestWithURL:oURL];
    NSHTTPURLResponse *oResponse;
    [NSURLConnection sendSynchronousRequest:oRequest returningResponse:&oResponse error:nil];
    if ( [oResponse respondsToSelector:@selector( allHeaderFields )] ) {
        NSDictionary *metaData = [oResponse allHeaderFields];
        return [metaData objectForKey:@"Last-Modified"];
    } else {
        return @"00000000";
    }
}

Problem 1: It returns "00000000" when given "http://www.mysite.com/myzip.zip" as a URL. Problem 2: For an Active Server Page (just a test; not that I'd really download one) it returns a date that has no bearing on the date the file was last uploaded or modified.

What's the right way?

+1  A: 

This is probably less a problem of your iPhone code but rather of the web server. A web server is not required to include the Last-Modified header attribute. And for dynamic pages (such as ASP pages) it is correct to return the date when the page was dynamically generated (i.e. the current date) and not the date when the page was developed or deployed.

I suggest you use a browser extension such as Live HTTP headers for Fireofx to observe what HTTP header attributes the web server returns. If the date is missing, you'll be out of luck (unless you have access to the web server and can fix it there).

Furthermore, your code will always download the image no matter when it has been last modified. You can prevent this if you include the HTTP header If-Modified-Since in your request. That way the web server will send the image if it has been modified since the specified date or just send a 304 (Not modified) result code if the image is still up to date. But again: It depends on the web server if this option is supported and it only works for static content unless the author of the web application has specifically implemented it.

Codo