2011-05-09 80 views
6

我知道我可以使用attributesOfItemAtPath获得除其他事项外文件的修改时间/日期......但有一种方法来设置的修改日期/时间一份文件?我看过How to set the modification time of a file programmatically?,但似乎并不适用。如何设置attributesOfItemAtPath(如修改日期/时间)iPhone

我问的原因是我使用http://allseeing-i.com/ASIHTTPRequest/(如下所示)来获取文件...但是时间戳不会与服务器上的最后修改时间保持一致。我想使用Last-Modified头文件来设置下载文件的日期/时间,这样我就可以将下载的时间保留在我的系统上,就像它在服务器上一样。

我的代码如下:

ASIHTTPRequest *requestFeed = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:kGZipSQLURL]]; 
[requestFeed setNumberOfTimesToRetryOnTimeout:2]; 
[requestFeed setDownloadDestinationPath:librarySQLGZipPath]; 
[requestFeed setCachePolicy:ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy]; 
[requestFeed setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy]; 
[requestFeed setSecondsToCache:60*60*24*30]; 
[requestFeed setDownloadCache:[ASIDownloadCache sharedCache]]; 
[requestFeed startSynchronous]; 
NSString *lastModified = [[requestFeed responseHeaders] objectForKey:@"Last-Modified"]; 
NSLog(@"Last Modified: %@",lastModified); 

因此,串lastModified持有时间/日期戳。有没有一种方法,以确保在librarySQLGZipPath文件将被设置在lastModified的日期/时间?使用当前的方法,文件librarySQLGZipPath保存下载的时间,这对我来说是无效的。

谢谢!

Jann

编辑后来:

因为我想很好地把这个页面上如何做这个代码,并仍想给史蒂芬克莱默信贷回答我现在要去编辑问题的答案我得到了使用代码:

NSDateFromRFC1123是在这里找到的例程:http://blog.mro.name/2009/08/nsdateformatter-http-header/(有一些调整)将Last-Modified标题字符串更改为NSDate对象:

NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:[self NSDateFromRFC1123:lastModified] ,NSFileModificationDate, nil]; 
NSError *errorFile; 
if ([NSFileManager defaultManager] setAttributes:attrs ofItemAtPath:librarySQLGZipPath error: &errorFile]) 
{ 
NSLog(@"Set timestamp of file"); 
} 
else { 
NSLog(@"COULD NOT set timestamp of file"); 
} 

非常感谢Steven!

回答

9

使用-[NSFileManager setAttributes:(NSDictionary *)attributes ofItemAtPath:(NSString *)path error:(NSError **)error

+0

啊...像http://stackoverflow.com/questions/5791148/set-date-modified-of-files-in-cocoa(这并没有拿出在搜索因为我放在iPhone中)谢谢指出我在正确的方向。对号和+1 – Jann 2011-05-09 16:01:42

+0

是啊,这似乎只是门票;-) – 2011-05-09 19:01:34

+0

谢谢,这帮助了我。 – 2015-06-05 14:35:57