16

我可以在模拟器中显示我的应用程序的构建日期,但是每当我归档应用程序并将其上传到 TestFlight,然后将其安装到设备上时,构建日期都不会显示。

这是我为显示构建日期所做的工作。

首先,我将CFBuildDate作为字符串添加到 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 文件中获取构建日期:

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

这会在模拟器中显示构建日期(尽管有时不会),但是通过 TestFlight 部署应用程序时,构建日期永远不会显示。有任何想法吗 ?

提前致谢。

4

7 回答 7

28

您可能会考虑使用内置__DATE____TIME__宏,它们将返回应用程序构建日期和时间的字符串表示形式。也许他们会对你有更多帮助:

NSString *dateStr = [NSString stringWithUTF8String:__DATE__];
NSString *timeStr = [NSString stringWithUTF8String:__TIME__];
于 2012-08-02T15:10:52.757 回答
12

要获取格式为“yyMMddHHmm”的构建日期,您可以尝试以下操作:

+ (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;
}
于 2013-07-15T11:05:28.040 回答
8

尝试将脚本作为构建阶段步骤运行,而不是方案预操作步骤,这样它就会一直运行,而不管您正在生成的构建类型如何。

于 2012-08-02T19:43:56.997 回答
7

斯威夫特3

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)
}
于 2016-11-14T19:54:03.487 回答
5

如果您重新定义__DATE__并且__TIME__ 每次构建应用程序时都会更新时间。您不需要清理或存档来更新时间,只需要运行项目。

  #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;
}
于 2015-12-11T13:23:37.723 回答
0

在 macOS 上,我使用此代码,它从应用程序的 Info.plist 内容中获取构建日期。可能也适用于iOS,我没有检查过:

+ (NSDate *)buildDate {
    static NSDate *result = nil;
    if (result == nil) { 
        NSDictionary *infoDictionary = NSBundle.mainBundle.infoDictionary;
        NSString *s = [infoDictionary valueForKey:@"BuildDateString"];
        NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
        NSDate *d = [formatter dateFromString:s];
        result = d;
    }
    return result;
}
于 2021-07-07T14:59:54.913 回答
-4

斯威夫特5

let compileDate = String(Date())
let df = DateFormatter()
df.dateFormat = "MMM-dd-yyyy"
let usLocale = NSLocale(localeIdentifier: "en_US")
df.locale = usLocale
let aDate: Date? = df.date(from: compileDate)
于 2019-07-23T18:36:05.177 回答