我想知道如何使用 ASIHTTP 检查和更新我的 iPhone 的文件
到目前为止我有:
__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];
我想检查缓存目录中的旧版本,并将 header 的最后修改时间与缓存目录中文件的修改日期进行比较,然后替换。
最佳答案
尝试这样的事情:
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
}
}
关于iPhone 检查和更新文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5380681/