xcode 如何向用户显示我的应用程序的当前项目版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1492351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:52:18  来源:igfitidea点击:

How to display the current project version of my App to the user?

iphonecocoacocoa-touchxcode

提问by jantimon

I would like to add the current version into the "about" section of my app. As seen in this attached screenshot Apple offers versioning.

我想将当前版本添加到我的应用程序的“关于”部分。如附加屏幕截图所示,Apple 提供版本控制。

How do you display these settings in your app?

您如何在您的应用中显示这些设置?

Screenshot of Target 'myApp' Info

目标“myApp”信息的屏幕截图

回答by jantimon

After further searching and testing, I found the solution myself.

经过进一步的搜索和测试,我自己找到了解决方案。

NSDictionary* infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSLog(@"%i Keys:  %@", [infoDictionary count],
             [[infoDictionary allKeys] componentsJoinedByString: @" ,"]);

This snipplet gave me the following output:

这个片段给了我以下输出:

20 Keys : NSBundleResolvedPath ,CFBundleVersion,NSBundleInitialPath ,CFBundleIdentifier ,NSMainNibFile ,CFBundleIconFile ,CFBundleInfoPlistURL ,CFBundleExecutable ,DTSDKName ,UIStatusBarStyle ,CFBundleDevelopmentRegion ,DTPlatformName ,CFBundleInfoDictionaryVersion ,CFBundleSupportedPlatforms ,CFBundleExecutablePath ,CFBundleDisplayName ,LSRequiresIPhoneOS ,CFBundlePackageType ,CFBundleSignature ,CFBundleName

20个键:NSBundleResolvedPath,CFBundleVersion,NSBundleInitialPath,CFBundleIdentifier,NSMainNibFile,CFBundleIconFile,CFBundleInfoPlistURL,CFBundleExecutable,DTSDKName,UIStatusBarStyle,CFBundleDevelopmentRegion,DTPlatformName,CFBundleInfoDictionaryVersion,CFBundleSupportedPlatforms,CFBundleExecutablePath,CFBundleDisplayName,LSRequiresIPhoneOS,CFBundlePackageType,CFBundleSignature,CFBundleName

So the solution is as simple as:

所以解决方案很简单:

NSString *version =[[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleVersion"];

However, this is notthe Current Project Version as seen in the screenshot but the Bundle Version of the plist file.

但是,这不是屏幕截图中看到的当前项目版本,而是 plist 文件的捆绑版本。

回答by Eimantas

Look into your Info.plistfile which should have keys like CFBundleVersionand CFBundleShortVersionString

查看你的Info.plist文件,它应该有像CFBundleVersionCFBundleShortVersionString

回答by mahboudz

Those items in the Build Info are not available to your built app. They are placeholders that you could possibly pull into your app. What is in your app is anything that you place in, say, the Resources folder of your app, like any text files, or plists, or a nice picture of your versioning engineer.

构建信息中的这些项目对您构建的应用程序不可用。它们是您可以拉入应用程序的占位符。应用程序中的内容是您放置在应用程序的 Resources 文件夹中的任何内容,例如任何文本文件、plist 或版本控制工程师的精美图片。

Now, you could pull some of the items in the Build Info window into a info.plist, using special identifiers, such as ${VERSION_INFO_PREFIX} or other token. The tokens are available if you click on any of the items on the left hand side in the window you have included above. For example, click on the word "Current Project Version" and copy the token that you see at the bottom, "CURRENT_PROJECT_VERSION". Then go to your plist file, and add an entry. Give it any name you want or "Current Project Version". Paste in ${CURRENT_PROJECT_VERSION} on the right hand side. Now that value is available to you from your app, programmatically. Of course, someone now has to enter that value into the appropriate place either in the Build Info window or elsewhere. It might just be easier just to manage this and fields like this in the info.plist file. It's up to you how you'd like to handle these things.

现在,您可以使用特殊标识符(例如 ${VERSION_INFO_PREFIX} 或其他标记)将 Build Info 窗口中的一些项目拉入 info.plist。如果您单击上面包含的窗口中左侧的任何项目,则可以使用令牌。例如,单击“当前项目版本”一词并复制您在底部看到的标记“CURRENT_PROJECT_VERSION”。然后转到您的 plist 文件,并添加一个条目。给它任何你想要的名字或“当前项目版本”。将 ${CURRENT_PROJECT_VERSION} 粘贴到右侧。现在,您可以通过您的应用程序以编程方式获得该值。当然,现在必须有人在 Build Info 窗口或其他地方的适当位置输入该值。在 info.plist 文件中管理这个和这样的字段可能会更容易。这取决于你想如何处理这些事情。

Here is how I get version info out of my info.plist:

以下是我从 info.plist 中获取版本信息的方法:

+ (NSString *) getAppVersionNumber;
{
    NSString    *myVersion,
                *buildNum,
                *versText;

    myVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    buildNum = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey];
    if (myVersion) {
        if (buildNum)
            versText = [NSString stringWithFormat:@"Version: %@ (%@)", myVersion, buildNum];
        else
            versText = [NSString stringWithFormat:@"Version: %@", myVersion];
    }
    else if (buildNum)
        versText = [NSString stringWithFormat:@"Version: %@", buildNum];
    NSLog(versText);
    return versText;
}

回答by Ilker Baltaci

  NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleVersion"];

returns me always the same value(initial version) even if i change the version from the project settings but.

即使我从项目设置中更改了版本,也总是返回相同的值(初始版本)。

  NSString * appVersionString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];

does the trick.

诀窍。

回答by Matthieu Riegler

For the lazy here is the swift version :

对于懒惰的人,这里是 swift 版本:

NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString")!

回答by Gabriel

NSBundle.mainBundle.infoDictionary[@"CFBundleShortVersionString"];

NSBundle.mainBundle.infoDictionary[@"CFBundleShortVersionString"];