jan*_*mon 18 iphone xcode cocoa cocoa-touch
我想将当前版本添加到我的应用程序的"关于"部分.如附带的屏幕截图所示,Apple提供版本控制.
如何在应用中显示这些设置?
jan*_*mon 25
经过进一步的搜索和测试,我自己找到了解决方案.
NSDictionary* infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSLog(@"%i Keys: %@", [infoDictionary count],
[[infoDictionary allKeys] componentsJoinedByString: @" ,"]);
Run Code Online (Sandbox Code Playgroud)
这个snipplet给了我以下输出:
20个键:NSBundleResolvedPath,CFBundleVersion,NSBundleInitialPath,CFBundleIdentifier,NSMainNibFile,CFBundleIconFile,CFBundleInfoPlistURL,CFBundleExecutable,DTSDKName,UIStatusBarStyle,CFBundleDevelopmentRegion,DTPlatformName,CFBundleInfoDictionaryVersion,CFBundleSupportedPlatforms,CFBundleExecutablePath,CFBundleDisplayName,LSRequiresIPhoneOS,CFBundlePackageType,CFBundleSignature,CFBundleName
所以解决方案很简单:
NSString *version =[[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleVersion"];
Run Code Online (Sandbox Code Playgroud)
但是,这不是屏幕截图中显示的当前项目版本,而是plist文件的Bundle版本.
Eim*_*tas 12
查看你的Info.plist文件应该有像CFBundleVersion和的键CFBundleShortVersionString
构建信息中的这些项目不适用于您构建的应用程序.它们是占位符,您可以将其拉入您的应用程序.您的应用程序中包含您放置在应用程序的Resources文件夹中的任何内容,例如任何文本文件或plist,或者版本工程师的精美图片.
现在,您可以使用特殊标识符(例如$ {VERSION_INFO_PREFIX}或其他标记)将"构建信息"窗口中的某些项目拉入info.plist.如果您单击上面包含的窗口左侧的任何项目,则可以使用标记.例如,单击"当前项目版本"一词并复制您在底部看到的令牌"CURRENT_PROJECT_VERSION".然后转到plist文件,并添加一个条目.给它任何你想要的名字或"当前项目版本".粘贴在右侧的$ {CURRENT_PROJECT_VERSION}中.现在,您可以通过编程方式从应用中获得该值.当然,有人现在必须在"构建信息"窗口或其他位置将该值输入适当的位置.在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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18038 次 |
| 最近记录: |