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

时间:2013-06-03 00:58:55

标签: ios objective-c

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

6 个答案:

答案 0 :(得分:271)

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

Swift 3

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

ObjC

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

Swift 2

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

和“Build”:

Swift 3

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

ObjC

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

Swift 2

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

答案 1 :(得分:84)

您可以尝试使用infoDictionary

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

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

答案 2 :(得分:11)

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]; 用于&#34;联系我们&#34;按钮,我使用此代码在用户的电子邮件上获取格式化的HTML。它为我提供了开始帮助解决任何问题所需的所有信息:

var fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.name = 'fileData';

var form = document.createElement('form');
form.method = 'post';
form.enctype = 'multipart/form-data';
form.appendChild(fileInput);

// Filter empty file inputs
var childNodes = form.querySelectorAll('input[type=file]');
for (var i=0;i<childNodes.length;i++) {
  if (childNodes[i].files.length === 0) {
    childNodes[i].parentElement.removeChild(childNodes[i]);
  }
}

var xhr = new XMLHttpRequest();
xhr.open("POST", "/url");
xhr.send(new FormData(form));

这是获取消息时的结果:

enter image description here

答案 3 :(得分:2)

Swift 3获取捆绑版本并构建字符串

代码

  extension UIApplication {
    class var Build: String? {
        get {
            return Bundle.main.infoDictionary?[kCFBundleVersionKey as String] as? String
        }
    }

    class var Version: String? {
        get {
            return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
        }
    }
}

用法

print("Version \(UIApplication.Version!)")
print("Build \(UIApplication.Build!)")

结果

项目设置

enter image description here

<强>控制台

enter image description here

答案 4 :(得分:1)

为了这个目的,我写了这个open source project。我的项目在发生重大事件时发布通知,例如用户第一次打开应用程序时,或者在升级后打开应用程序(包括用户升级的版本信息)。来源很简单,应该很容易理解。如果您有任何问题/要求,请与我们联系。

我最近还写了一篇关于它的blog post

答案 5 :(得分:1)

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

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

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

NSString *buildVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];