How to request last modified file date via HTTP on iPhone using NSHTTPURLResponse?

In my iPhone application, I need to request the latest modification date of the .m4a Internet file via HTTP , but I DO NOT want to download it.

I am reading Apple's documentation on NSURLRequest and NSHTTPURLResponse , but it all seems to be related to downloading the file, not its request. Maybe I'm wrong.

How to find out the latest modification date of a .m4a file using HTTP WITHOUT downloading it?

Thanks!

+6
source share
2 answers

This answer assumes that your server supports it, but what you are doing is sending a β€œHEAD” request to the file URL, and you will only return the file headers. You can then look at the "Last-Modified" heading, which usually has the date format @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'" .

Here is the code:

 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"HEAD"]; NSHTTPURLResponse *response; [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; if ([response respondsToSelector:@selector(allHeaderFields)]) { NSDictionary *dictionary = [response allHeaderFields]; NSString *lastUpdated = [dictionary valueForKey:@"Last-Modified"]; NSDate *lastUpdatedServer = [fileDateFormatter dateFromString:lastUpdated]; if (([localCreateDate earlierDate:lastUpdatedServer] == localCreateDate) && lastUpdatedServer) { NSLog(@"local file is outdated: %@ ", localPath); isLatest = NO; } else { NSLog(@"local file is current: %@ ", localPath); } } else { NSLog(@"Failed to get server response headers"); } 

Of course, you probably want this to be done asynchronously in the background, but this code should point you in the right direction.

Best wishes.

+9
source

The following method executes a HEAD request only to get the header with the Last-Modified field and converts it to an NSDate object.

 - (NSDate *)lastModificationDateOfFileAtURL:(NSURL *)url { NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; // Set the HTTP method to HEAD to only get the header. request.HTTPMethod = @"HEAD"; NSHTTPURLResponse *response = nil; NSError *error = nil; [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; if (error) { NSLog(@"Error: %@", error.localizedDescription); return nil; } else if([response respondsToSelector:@selector(allHeaderFields)]) { NSDictionary *headerFields = [response allHeaderFields]; NSString *lastModification = [headerFields objectForKey:@"Last-Modified"]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss zzz"]; return [formatter dateFromString:lastModification]; } return nil; } 

You must run this method asynchronously in the background so that the main thread does not block while waiting for a response. This can be easily done using a couple of GCD lines.

The code below makes a call to get the latest change date in the background thread and calls the completion block in the main thread when it receives the date.

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ { // Perform a call on the background thread. NSURL *url = [NSURL URLWithString:@"yourFileURL"]; NSDate *lastModifDate = [self lastModificationDateOfFileAtURL:url]; dispatch_async(dispatch_get_main_queue(), ^ { // Do stuff with lastModifDate on the main thread. }); }); 

I wrote an article about this here:

Getting the latest modification date of a file on the server using Objective-C.

+3
source

Source: https://habr.com/ru/post/917217/


All Articles