notjrbauer
notjrbauer

Reputation: 71

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.

Upvotes: 4

Views: 2208

Answers (2)

leviathan
leviathan

Reputation: 11161

Since you're using a download cache, it will do this for you. When the request completes, the response should be the most current version, and the request will only download the actual content again if it has been updated.

and how would I save this on the iphone?

The simplest approach is just to set a downloadDestinationPath on the request, and it will save it in a file for you.

Upvotes: 0

MCannon
MCannon

Reputation: 4041

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
    }
}

Upvotes: 8

Related Questions