显示应用的生成date

我能够在模拟器中显示我的应用程序的生成date,但每当我存档应用程序,并将其上传到TestFlight,然后将其安装在设备上,生成date不显示。

这是我正在做的显示生成date。

首先,我将CFBuildDate作为string添加到myproject-info.plist

接下来,我将下面的脚本添加到Edit Scheme – > Build – > Pre-Actions – > Run Script Action:

infoplist="$BUILT_PRODUCTS_DIR/$INFOPLIST_PATH" builddate=`date` if [[ -n "$builddate" ]]; then /usr/libexec/PlistBuddy -c "Add :CFBuildDate $builddate" ${infoplist} /usr/libexec/PlistBuddy -c "Set :CFBuildDate $builddate" ${infoplist} fi 

最后,使用下面的代码从plist文件获取生成date:

 NSString *build_date = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBuildDate"]; 

这会在模拟器中显示生成date(尽pipe偶尔不会),但是通过TestFlight部署应用程序时,生成date从不显示。 有任何想法吗 ?

提前致谢。

尝试运行脚本作为构build阶段步骤,而不是一个scheme预执行步骤,因此它始终运行,无论您正在生成的构buildtypes如何。

您可能会考虑使用内置的__DATE____TIME__macros,它们将返回构build应用程序的date和时间的string表示forms。 也许他们会对你有更多的帮助:

 NSString *dateStr = [NSString stringWithUTF8String:__DATE__]; NSString *timeStr = [NSString stringWithUTF8String:__TIME__]; 

要获取格式为“yyMMddHHmm”的生成date,可以试试这个:

 + (NSString *)GetBuildDate { NSString *buildDate; // Get build date and time, format to 'yyMMddHHmm' NSString *dateStr = [NSString stringWithFormat:@"%@ %@", [NSString stringWithUTF8String:__DATE__], [NSString stringWithUTF8String:__TIME__]]; // Convert to date NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"LLL d yyyy HH:mm:ss"]; NSDate *date = [dateFormat dateFromString:dateStr]; // Set output format and convert to string [dateFormat setDateFormat:@"yyMMddHHmm"]; buildDate = [dateFormat stringFromDate:date]; [dateFormat release]; return buildDate; } 

如果您重新定义__DATE____TIME__ ,则每次构build应用程序时都会更新时间。 你不需要清理或存档更新时间,只需要运行项目。

  #define DATE [NSString stringWithUTF8String:__DATE__] #define TIME [NSString stringWithUTF8String:__TIME__] - (NSString *)getBuildDate { NSString *buildDate; // Get build date and time, format to 'yyMMddHHmm' NSString *dateStr = [NSString stringWithFormat:@"%@ %@", DATE , TIME ]; // Convert to date NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"LLL d yyyy HH:mm:ss"]; NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; [dateFormat setLocale:usLocale]; NSDate *date = [dateFormat dateFromString:dateStr]; // Set output format and convert to string [dateFormat setDateFormat:@"dd/MM/yyyy-HH:mm"]; buildDate = [dateFormat stringFromDate:date]; return buildDate; } 

Swift3

 static var buildDate: Date? { guard let infoPath = Bundle.main.path(forResource: "Info.plist", ofType: nil) else { return nil } guard let infoAttr = try? FileManager.default.attributesOfItem(atPath: infoPath) else { return nil } let key = FileAttributeKey(rawValue: "NSFileCreationDate") guard let infoDate = infoAttr[key] as? Date else { return nil } return infoDate } static var prettyBuildDate: String { guard let date = buildDate else { return "Unknown" } let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ" formatter.timeZone = TimeZone(abbreviation: "UTC") return formatter.string(from: date) }