iOS应用,以编程方式获取构建版本


Answers:


284

您在Xcode目标摘要的“版本”字段中设置的值位于此处:

迅捷3

let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String

对象

NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

迅捷2

let version = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as! String

和“构建”:

迅捷3

let build = Bundle.main.infoDictionary?[kCFBundleVersionKey as String] as? String

对象

NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];

迅捷2

let build = NSBundle.mainBundle().infoDictionary?[kCFBundleVersionKey as String] as! String

5
使用字符串文字而不是常量不能保证未来的发展。有关常数键,请参见Anupdas的答案。
亚伦·布拉格

9
您正确使用@“ CFBundleVersion”-我已经编辑了答案。但是,对于@“ CFBundleShortVersionString”,没有常量AFAIK。
e1985

对于Swift,NSBundle.mainBundle()现在是Bundle.main
Todd

85

您可以尝试使用infoDictionary

NSDictionary *infoDictionary = [[NSBundle mainBundle]infoDictionary];

NSString *version = infoDictionary[@"CFBundleShortVersionString"];
NSString *build = infoDictionary[(NSString*)kCFBundleVersionKey];
NSString *bundleName = infoDictionary[(NSString *)kCFBundleNameKey]; 

13
kCFBundleVersionKey是“构建”不“版本”
powerj1984

11

使用时 MFMailComposeViewController一个“联系我们”按钮,我用这个代码来获得一个使用HTML的电子邮件从用户。它为我提供了开始解决任何问题所需的所有信息:

struct utsname systemInfo;
uname(&systemInfo);

NSDateFormatter *formatter = [[NSDateFormatter alloc]  init];
NSDate  *date = [NSDate date];
[formatter setDateFormat:@"MM/dd/yyyy 'at' hh:mm a"];
NSString *dateString = [formatter stringFromDate:date];

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width - 65.0;

NSString *comments = NSLocalizedString(@"Please write your comments below:", nil);
NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleNameKey];
NSString *deviceModel = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
NSString *iOSVersion = [[UIDevice currentDevice] systemVersion];

NSString *emailBody = [NSString stringWithFormat:@"<!DOCTYPE html><html><style> .div {background-color: lightgrey; width: %fpx; padding: 10px; border: 5px solid navy; margin: 2px;}</style><body><div class='div'><p><b>App:</b> %@</p><b><p>Device:</b> %@</p><b><p>iOS Version:</b> %@</p><b><p><p>App Version and Build:</b> %@ (%@)</p><b><p>Date:</b> %@</p> </p></div><font color='red'><b>%@</b></font><br><br></body></html>",screenWidth,appName,deviceModel,iOSVersion,version,build,dateString,comments];

[self setMessageBody:emailBody isHTML:YES];

这是收到消息时的结果:

在此处输入图片说明


3

两者的Swift版本分别:

let versionNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
let buildNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleVersion") as! String

它包含在此仓库中,请查看:

https://github.com/goktugyil/EZSwiftExtensions


您应该明确说明自己拥有该存储库。您不希望成为垃圾邮件发送者。
2016年

2

细节

  • Xcode版本10.3(10G8),Swift 5

class Info {
    static let dictionary = Bundle.main.infoDictionary ?? [:]
    enum Value {
        case build, version
    }
}

extension Info.Value {

    var key: String {
        switch self {
            case .build: return kCFBundleVersionKey as String
            case .version: return kCFBundleInfoDictionaryVersionKey as String
        }
    }

    var string: String? { return Info.dictionary[key] as? String }
}

用法

if let value = Info.Value.version.string { print("Version: \(value)") }
if let value = Info.Value.build.string { print("Build: \(value)") }

结果

项目设定

在此处输入图片说明


1

我已经写了这个开源项目正是出于这个目的。我的项目会在发生重大事件时发布通知,例如,当用户首次打开应用程序或在升级后打开应用程序时(包含有关用户从哪个版本升级的信息)。来源很简单,应该很容易理解。让我知道您是否有任何疑问/要求。

我最近也写了一篇关于它的博客文章


1

1)要获取App版本,您必须使用:

NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

2)要获得Build版本,您必须使用:

NSString *buildVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.