132

有没有办法以编程方式获取我的应用程序的构建版本?我需要能够检测到用户何时通过 AppStore 更新了应用程序,以便执行一些代码进行调整

4

7 回答 7

297

您在 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
于 2013-06-03T01:37:56.677 回答
85

您可以尝试使用 infoDictionary

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

NSString *version = infoDictionary[@"CFBundleShortVersionString"];
NSString *build = infoDictionary[(NSString*)kCFBundleVersionKey];
NSString *bundleName = infoDictionary[(NSString *)kCFBundleNameKey]; 
于 2013-06-03T01:00:53.357 回答
12

当使用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];

这是收到消息时的结果:

在此处输入图像描述

于 2016-03-10T15:10:12.853 回答
4

两者分别的 Swift 版本:

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

它包含在此回购中,请查看:

https://github.com/goktugyil/EZSwiftExtensions

于 2015-11-24T03:41:59.047 回答
2

1)要获得应用程序版本,您必须使用:

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

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

NSString *buildVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
于 2016-07-06T13:04:09.957 回答
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)") }

结果

项目设置

在此处输入图像描述

于 2016-11-28T17:25:24.233 回答
1

我正是为此目的编写了这个开源项目。我的项目在发生重大事件时发布通知,例如当用户第一次打开应用程序或升级后打开应用程序时(包括用户从哪个版本升级的信息)。来源很简单,应该很容易理解。如果您有任何问题/要求,请告诉我。

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

于 2013-06-03T02:09:46.193 回答