Advertisement

Comparing Header of Server File with Stored File Header

Here’s a frequent function necessary for checking the most-up-to-date content. Note that KURL is a constant string defined elsewhere:

    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,

                                                                   NSUserDomainMask, YES)

                               objectAtIndex:0];

    NSString *isiSavedFilePath = [documentsPath

                                  stringByAppendingPathComponent:@”myFile.pdf”];

    

    NSURL *isiURL = [NSURL fileURLWithPath:isiSavedFilePath];

    

    // if file doesn’t exist, download it

    

    if (![[NSFileManager defaultManager] fileExistsAtPath:isiSavedFilePath] &&

        [self isConnected]) {

        NSData *pdfData =

            [[NSDataalloc] initWithContentsOfURL:[NSURLURLWithString:KURL]];

        //Store the downloaded file  in documents directory as a NSData format

        

        [pdfData writeToFile:isiSavedFilePath atomically:YES];

    } else {

        if ([self isConnected]) {

        // if the file is stored, compare the file modified date.

        // create a HTTP request to get the file information from the web server

        

        NSURL* url = [NSURL URLWithString:KISIURL];

        NSMutableURLRequest* request = [NSMutableURLRequestrequestWithURL:url];

        [request setHTTPMethod:@”HEAD”];

        

        NSHTTPURLResponse* response;

        [NSURLConnectionsendSynchronousRequest:request

                              returningResponse:&response error:nil];

        

        // get the last modified info from the HTTP header

        

        NSString *httpLastModified = nil;

        if ([response respondsToSelector:@selector(allHeaderFields)]) {

            httpLastModified = [[response allHeaderFields]

                                objectForKey:@”Last-Modified”];

        }

        

        // setup a date formatter to query the server file’s modified date

        

        NSDateFormatter *df = [[NSDateFormatteralloc] init];

        df.dateFormat = @”EEE’,’ dd MMM yyyy HH’:’mm’:’ss ‘GMT'”;

        df.locale = [[NSLocalealloc] initWithLocaleIdentifier:@”en_US”];

        df.timeZone = [NSTimeZonetimeZoneWithAbbreviation:@”GMT”];

        

        // get the file attributes to retrieve the local file’s modified date

        

        NSDictionary *fileAttributes =

        [[NSFileManagerdefaultManager] attributesOfItemAtPath:isiSavedFilePath error:nil];

        

        // test if the server file’s date is later than the local file’s date

        

        NSDate *serverFileDate = [df dateFromString:httpLastModified];

        NSDate *localFileDate = [fileAttributes fileModificationDate];

        BOOL isServerNewer = ([localFileDate laterDate:serverFileDate] == serverFileDate);

        NSLog(@”isServerNewer = %d”, isServerNewer);

        

        // and then replace it if it is newer.

        

        if (isServerNewer) {

            NSError *error;

            [[NSFileManager defaultManager] removeItemAtPath:isiSavedFilePath error:&error];

            NSData *pdfData =

            [[NSDataalloc] initWithContentsOfURL:[NSURLURLWithString:KURL]];

            //Store the downloaded file  in documents directory as a NSData format

            [pdfData writeToFile:isiSavedFilePath atomically:YES];

        }

        } else {

            [[[UIAlertViewalloc] initWithTitle:@”No Internet”message:@”Not able to download the file until you connect to the Internet.”delegate:selfcancelButtonTitle:@”OK”otherButtonTitles:nil] show];

        }

    }