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

y53ybaqx  于 2023-05-30  发布在  iOS
关注(0)|答案(7)|浏览(171)

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

moiiocjp

moiiocjp1#

您在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

关于“构建”:
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
7qhs6swi

7qhs6swi2#

您可以尝试使用infoDictionary

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

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

9rbhqvlz3#

当使用MFMailComposeViewController作为“联系我们”按钮时,我使用这段代码在用户的电子邮件中获取格式化的HTML。它给了我所有的信息,我需要开始帮助解决任何问题:

#import <sys/utsname.h> // Don't forget to import that somewhere //

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];

这是获得消息时的结果:

utugiqy6

utugiqy64#

Swift版本分别为:

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

它包含在这个repo中,检查一下:
https://github.com/goktugyil/EZSwiftExtensions

dzhpxtsq

dzhpxtsq5#

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

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

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

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

ttcibm8c6#

详情

  • 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)") }

结果

项目设置

gajydyqb

gajydyqb7#

我写这个open source project正是为了这个目的。我的项目会在发生重大事件时发布通知,例如用户首次打开应用程序或在升级后打开应用程序时(包括用户从哪个版本升级的信息)。来源很简单,应该很容易理解。如果你有任何问题/要求,请告诉我。
我最近也写了一个关于它的blog post

相关问题