If you rely on updating files stored on a server, you want a way to find out when a file has been updated on that server, so you can download the latest version.
The easiest way I’ve found is using NSURLConnection’s sendSynchronousRequest:returningResponse:error method, but using a HEAD http method instead of GET. Here’s a simple example:
+ (NSDate*)getFileDate:(NSString*)httpFilePath { NSURL *url=[NSURL URLWithString:httpFilePath]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"HEAD"]; NSHTTPURLResponse *response; [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL]; if (response==nil) return nil; NSDate *lastModifiedDate=nil; NSString *lastModified=[[response allHeaderFields] objectForKey:@"Last-Modified"]; @try { NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease]; df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'"; df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; lastModifiedDate = [df dateFromString:lastModified]; } @catch (NSException * e) { NSLog(@"Error formatting Last-Modified date: %@ (%@)", lastModified, [e description]); } return lastModifiedDate; }
For completeness sake, to grab a file, we’d use code like this:
+ (NSData*)getFileData:(NSString*)httpFilePath { NSURL *url=[NSURL URLWithString:httpFilePath]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"GET"]; NSHTTPURLResponse *response; NSData *result=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL]; if (response==nil || response.statusCode>400) return nil; return result; }
You can do something similar asynchronously, but its a bit more complicated.