IPhone Check and Update File

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

so far I:

__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 the cache directory and compare the latest header change with the changed file date in the cache directory and then replace.

+4
source share
2 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 } } 
+8
source

Since you are using the download cache, it will do it for you. When the request completes, the response must be the latest version, and the request will only download the actual content again if it has been updated.

and how can i save this on iphone?

The easiest way is to simply set downloadDestinationPath in the request and it will save it in a file for you.

0
source

Source: https://habr.com/ru/post/1344634/


All Articles