Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone Checking and Updating File

I was wondering how to check and update a file for my iPhone using ASIHTTP

so far i have:

__block BOOL complete = NO;
__block BOOL failed = NO;
/* doing the actual check. replace your existing code with this. */
NSURL *url = [NSURL URLWithString:@"http://notasdasd.com/file.txt"];
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCachePolicy:ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
[request setCompletionBlock:^{complete = YES;}];
[request setFailedBlock:^{failed = YES;}];
[request startSynchronous];
NSString *latestText = [request responseString];

And I want to check the old version in cache directory and compare the last modified of the headers to the modified date of the file in cache directory and then replace.

like image 977
notjrbauer Avatar asked Feb 25 '23 18:02

notjrbauer


1 Answers

try something like this:

if([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
    NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error];
    NSDate *fileDate =[dictionary objectForKey:NSFileModificationDate];
    NSDate *today =[NSDate date];
    if (![[[today dateByAddingTimeInterval:-(60*60*24)] earlierDate:fileDate] isEqualToDate:fileDate]) {
        //file is less than 24 hours old, use this file.
    }
    else{
        //file is older than 24 hours, replace it
    }
}
like image 85
MCannon Avatar answered Mar 03 '23 01:03

MCannon