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

jca*_*ete 124 objective-c ios

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

e19*_*985 277

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

斯威夫特3

let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String
Run Code Online (Sandbox Code Playgroud)

ObjC

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

斯威夫特2

let version = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as! String
Run Code Online (Sandbox Code Playgroud)

和"构建":

斯威夫特3

let build = Bundle.main.infoDictionary?[kCFBundleVersionKey as String] as? String
Run Code Online (Sandbox Code Playgroud)

ObjC

NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];
Run Code Online (Sandbox Code Playgroud)

斯威夫特2

let build = NSBundle.mainBundle().infoDictionary?[kCFBundleVersionKey as String] as! String
Run Code Online (Sandbox Code Playgroud)

  • 你是对的@"CFBundleVersion" - 我编辑了我的答案.但是对于@"CFBundleShortVersionString",没有恒定的AFAIK. (9认同)
  • 使用字符串文字而不是常量不是面向未来的; 看到Anupdas对常量键的回答. (5认同)

Anu*_*das 84

您可以尝试使用infoDictionary

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

NSString *version = infoDictionary[@"CFBundleShortVersionString"];
NSString *build = infoDictionary[(NSString*)kCFBundleVersionKey];
NSString *bundleName = infoDictionary[(NSString *)kCFBundleNameKey]; 
Run Code Online (Sandbox Code Playgroud)

  • kCFBundleVersionKey是'build'而不是'version' (13认同)

Win*_*ton 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];
Run Code Online (Sandbox Code Playgroud)

这是获取消息时的结果:

在此输入图像描述