2011-05-09 81 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