显示应用程序的构建日期

Myx*_*tic 15 xcode build objective-c ios

我能够在模拟器中显示我的应用程序的构建日期,但每当我存档应用程序并将其上传到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
Run Code Online (Sandbox Code Playgroud)

最后,使用以下代码从plist文件中获取构建日期:

NSString *build_date = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBuildDate"];
Run Code Online (Sandbox Code Playgroud)

这会在模拟器中显示构建日期(虽然偶尔也没有),但是当通过TestFlight部署应用程序时,构建日期永远不会显示.有任何想法吗 ?

提前致谢.

Jer*_*emy 27

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

NSString *dateStr = [NSString stringWithUTF8String:__DATE__];
NSString *timeStr = [NSString stringWithUTF8String:__TIME__];
Run Code Online (Sandbox Code Playgroud)

  • 为了使整个项目的构建日期和时间,我添加了一个预构建脚本行与此代码触摸文件,否则时间戳是上次修改该文件的时间,而不是构建的时间戳 (2认同)

小智 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;
}
Run Code Online (Sandbox Code Playgroud)

  • 不错:)但不要忘记设置区域设置,否则它无处不在.[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US"]]; (2认同)

tro*_*foe 8

尝试将脚本作为构建阶段步骤而不是方案预执行步骤运行,因此无论您生成的构建类型如何,它都会一直运行.


neo*_*eye 7

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)
}
Run Code Online (Sandbox Code Playgroud)


Ped*_*dro 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;
}
Run Code Online (Sandbox Code Playgroud)