AFAIK無法讓XCode保留日期。我遇到了一個類似的問題,並提出了一個shell腳本來製作一個plist文件,其中包含文件在目錄中的修改日期(遞歸)。然後我將它包含在我的包中,並在複製文件時設置修改的日期來讀取它。您可以修改這個來處理創建或訪問日期,或者通過一些組合工作。
這裏的bash腳本:(我還是新的bash的,所以請就如何讓它變得更好,歡迎任何意見)
#!/bin/bash
#First parameter is the folder to recurse into, defaults to current directory
#Second parameter is the full path to a pList file without the extension, defaults to
#<CURRENT DIRECTORY>/fileInfo
#if you don't specify /'s in the parameter it will be treated as a domain instead and
#the pList will be elsewhere...
pinfoLoc=${2:-`pwd`/fileInfo}
#Useful for debugging
#echo Source: ${1:-.}
#echo Destination: $pinfoLoc
for i in ${1:-.}/*; do
#Format: %Sa = last access, %Sm = last modified, %Sc = created
date=`stat -f "%Sm" $i`
#Uncomment below to see each file and date used
#echo $i - $date
#Write the data to the pList
defaults write $pinfoLoc "$i" -date "$date"
#Recurse into directories, not sure if there's a better way to do this
if [ -d "$i" ]; then
bash $0 $i $pinfoLoc
fi
done
基本上我稱之爲「資產」的文件夾是生活之外並鏈接到它,XCode會將該文件夾複製到我的包中。這裏是我想出的代碼,用於更新每個文件的修改日期AFTER我已經從包中複製了它們。
//Update file properties
NSString *file, *dstPath, *srcPath;
NSDictionary *fAttribs;
NSData *fInfoData = [fm contentsAtPath:[[NSBundle mainBundle] pathForResource:@"fileInfo" ofType:@"plist"]];
NSPropertyListFormat format;
NSDictionary *fileInfo = (NSDictionary *)[NSPropertyListSerialization
propertyListWithData:fInfoData
options:NSPropertyListMutableContainersAndLeaves
format:&format
error:&error];
NSString * resPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"assets"];
NSDirectoryEnumerator *dirEnum = [fm enumeratorAtPath:resPath];
while (file = [dirEnum nextObject])
{
NSLog(@"Directory Enum File: %@", file);
dstPath = [NSString pathWithComponents:@[[appInfo appSupportPath], @"res", file ]];
srcPath = [@"assets" stringByAppendingPathComponent:file];
fAttribs = @{ NSFileModificationDate: [fileInfo valueForKey:srcPath]};
[fm setAttributes:fAttribs ofItemAtPath:dstPath error:&error];
if (error)
{
NSLog(@"Error setting attribute to file: %@ - %@", file, error.localizedDescription);
[error release];
error = nil;
}
}
這是我的複製文件方法後,有幾個變量,你看不到。即fm
只是NSFileManager
的默認管理器,error
是未初始化的NSError
,[appInfo appSupportPath]
是一種靜態方法,它返回存儲設備上的東西的目錄(因爲您無法修改該包)。
此外,我硬編碼的日期,以及我已經複製我的文件的目錄的位置,我可能會稍後在我的代碼中更新。
我做的下一件事是設置XCode自動運行我的腳本並且還包含fileInfo.plist。後者很容易,一旦你運行了腳本,你可以將它拖放到Xcode中並保存爲參考,棘手的部分是讓它在每個版本上運行腳本。
- 複製/粘貼我的bash腳本到上面的Project Navigator
- 您的項目文件
- 點擊,如果它尚未選定
- 下點擊目標選擇目標(應用程序名稱)在構建階段選項卡
- 在底部上點擊右鍵添加構建階段 - >添加運行腳本
- 的新階段將是最後的事情,可惜就是後它將文件複製到包中,包括我們正在生成的文件。因此,相拖到略高於「複製包資源」
- 從shell我把/斌/ bash中,/ bin/sh的可能工作以及...
在下面的行我把這個:
pushd $PROJECT_DIR/..
/PATH/TO/BASH/FILE/ABOVE.sh assets
popd
pushd $PROJECT_DIR/..
是需要的,因爲資產目錄比我的項目高一級,我可以硬編碼目錄去代替,可能會更清楚。
- 如果您現在嘗試構建,您可能會因爲腳本文件沒有執行權限而收到權限錯誤。我不知道一個更簡單的方法在Mac上設置此比打開終端並運行
chmod +x <BASH FILE NAME>
是的我意識到,允許每個人都執行它,如果你擔心它然後玩權限,我不是肯定如果XCode構建爲一個不同的用戶或什麼。
希望至少有一些幫助。我使用這個修改日期來查看Web服務器上是否有更新的文件,並只下載我需要的文件。