1

Our requirements include checking Internet access to a specific file on the web-server. This file is checked every n minutes. NSURLRequests never calls connection:didFailWithError whether or not there is an internet connection. And the HTTP status is always 200. Apple's reachibility only works for domains, not files- so it doesn't meet the requirements. How can I reliably discover if I can reach this file every n minutes? Why isn't the http status code really the http status code?

Other stackoverflow questions that would seem to answer this question do not work:
1. How could connectionDidFinishLoading: run if no file is found on server?
2. Testing use of NSURLConnection with HTTP response error statuses

I tried using another queue with a completion block, but that also didn't work.

-(void) updateConnectionStatus 
{   
    NSURL *url = [NSURL URLWithString:(NSString*)[appValues getValueForSettingsKey:@"company.project.test.pingURL"]];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    //NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    //__block __typeof__(self) _self = self;
    connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
 /*
    [NSURLConnection
    sendAsynchronousRequest:urlRequest queue:queue
        completionHandler:^(NSURLResponse *response,
                         NSData *data,
                         NSError *error) {

         NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
         int code = [httpResponse statusCode]; // ALWAYS 200 no matter what
         NSString *pingFile = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
         NSLog(@"%@",error); // NEVER has an error
         //This doesn't even work because it remembers FOREVER the value once it gets it.
         if ([@"Ping!" isEqualToString:pingFile])  
         {
             dispatch_async(dispatch_get_main_queue(), ^{                     
                 [_self companyConnection:YES];                     
             });                 
         } else {
             dispatch_async(dispatch_get_main_queue(), ^{                     
                 [_self companyConnection:NO];                     
             });
         }         
     }];
      */
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"ERROR:  %@", error); // Never get here 
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *aResponse = (NSHTTPURLResponse*)response;
    NSLog(@"received a response: %ld",(long)[aResponse statusCode] );
    if ([response respondsToSelector:@selector(statusCode)])
    {
        int statusCode = [((NSHTTPURLResponse *)response) statusCode];
        // statusCode is always 200
        if (statusCode >= 400)
        {
            [companyConnection cancel];  // stop connecting; no more delegate messages
            NSDictionary *errorInfo
            = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:
                                                  NSLocalizedString(@"Server returned status code %d",@""),
                                                  statusCode]
                                          forKey:NSLocalizedDescriptionKey];
        }
    }
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"received data");
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Finished");        
}
Community
  • 1
  • 1
William Chadwick
  • 134
  • 2
  • 15
  • Are you doing anything to prevent caching of the first response you receive? – Wain May 16 '13 at 21:36
  • How would I prevent caching of the first response? since there are no variables outside the scope of each method it would seem that those are gone by the time the next request comes through. I'm using ARC by the way. – William Chadwick May 16 '13 at 21:39
  • Assuming you're running on a recent version of iOS, NSURLCache will automatically cache responses (depending on the returned headers). – Wain May 16 '13 at 21:46
  • If you implement [willCacheResponse](http://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLConnectionDataDelegate_protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40011348-CH1-SW12) and return `nil`, that should also disable cacheing. – Rob May 16 '13 at 21:58
  • Wain, if you post an answer I will mark it as accepted. Because your comment led me to the solution. – William Chadwick May 16 '13 at 22:20

2 Answers2

0

Try with setting cachePolicy as NSURLRequestReloadIgnoringCacheData while constructing the NSURLRequest object

bademi
  • 421
  • 5
  • 8
0

Thanks to Wain and Rob for putting me onto the right path. One way to keep the cache clear is adding this method to your NSURLConnectionDelegate:

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
    return nil;
}
William Chadwick
  • 134
  • 2
  • 15