Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Send If Modified Since header with request

Does anyone know how to send If Modified Since header with a url request?

I'm trying to get a status code back that will tell me if an RSS page has been updated or not.

UPDATE #1: I was able to do a test doing the following:

NSURL *url = [NSURL URLWithString:self.url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request addValue:@"Tue, 18 Nov 2014 20:46:46 GMT" forHTTPHeaderField:@"If-Modified-Since"];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

    int responseCode = (int)[httpResponse statusCode];
    NSLog(@"All headers: %@", [httpResponse allHeaderFields]);
    NSLog(@"Status code:: %d", responseCode);
}];

What's weird though, if I delete my entire iOS application and run it I get the 304 status code. But, if I comment out the line where I add the If-Modified-Since value and re-run it -- then I get the 200 but when I uncomment that line and re-run it -- then the 304 error doesn't come back... It's a 200 error code. The only way to get the 304 error back is by deleting the entire application off my iPhone device.

Any ideas what's going on?

like image 967
Sandy D. Avatar asked Sep 17 '25 16:09

Sandy D.


1 Answers

A little late with this but since NSURLConnection is caching the responses in order to get the 304 code you should create your request like this.

NSURLRequest *request = [NSURLRequest requestWithURL:url
                                         cachePolicy:NSURLRequestReloadIgnoringCacheData
                                     timeoutInterval:60.0];
like image 134
dminones Avatar answered Sep 20 '25 09:09

dminones