在ios设置包中输出简单的标题

我只想在设置文件中输出我的ios应用程序的版本号。

我明白,我必须将设置文件添加到应用程序文件夹。

当我build立和运行,我可以看到标准设置捆绑来的4个设置。

为了得到一个简单的只读string,我将第二个值更改为以下

在这里输入图像说明

在代码(didFinishLaunchingWithOptions :)我打电话如下:

NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]; [[NSUserDefaults standardUserDefaults] setValue:version forKey:@"version_number"]; [[NSUserDefaults standardUserDefaults] synchronize]; 

令我惊讶的是,一切都没有发生。 我只看到组元素和toogle开关和滑块,但没有标题行。 有人知道我错过了什么?

非常感谢你!

好的,我也有这个问题。 解决scheme(sorting)是提供一个默认值字段并给出一个值。 这实际上是在文档中明确说明的 – 默认值是Title属性的必填字段,所以如果不指定它,标题将不会显示在设置窗格中。 不幸的是,我似乎无法改变价值一旦设置,也可能如devise – 文件还指出,这是一个只读属性。 我将要尝试的解决scheme是每次进行新构build时,都明确地将版本号放在我的Root.plist文件中。 SUPER不理想,但我认为会起作用。

编辑:看看这个post更新设置包中的版本号

编辑:好吧,我得到了这个工作(感谢上面的post,以及一些与bash脚本有关的黑客行为,这是我很less有经验的)。这里是脚本(我刚刚在“运行脚本”内联编写相):

 #!/bin/bash builtInfoPlistPath=${TARGET_BUILD_DIR}/${INFOPLIST_PATH} #increment the build number buildNumber=$(/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" "$builtInfoPlistPath") buildNumber=$(($buildNumber + 1)) /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$builtInfoPlistPath" #compose the version number string versionString=$(/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "$builtInfoPlistPath") versionString+=" (" versionString+=$(/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" "$builtInfoPlistPath") versionString+=")" #write the version number string to the settings bundle #IMPORTANT: this assumes the version number is the first property in the settings bundle! /usr/libexec/PlistBuddy -c "Set :PreferenceSpecifiers:0:DefaultValue $versionString" "Settings.bundle/Root.plist" 

…就是这样! 奇迹般有效! 希望能帮助你解决问题,因为它解决了我的问题。 现在唯一的问题是与内部版本号略有差异…

编辑:…我用这个post的vakio的第二个评论固定,而不是设置到info.plist的path已经处理(在运行脚本阶段之前!)的path!

编辑:这是我的更新版本,它是在一个外部文件,并validation一些源文件已更改之前递增内部版本号:

  #!/bin/bash #note: for simplicity, it's assumed that there's already a bundle version (which is an integer) and a version string. set them in the Summary pane! #get path to the BUILT .plist, NOT the packaged one! this fixes the off-by-one bug builtInfoPlistPath=${TARGET_BUILD_DIR}/${INFOPLIST_PATH} echo "using plist at $builtInfoPlistPath" modifiedFilesExist=false #this is the modification date to compare to -- there's a possible bug here, if you edit the built plist directly, for some reason. probably you shouldn't do that anyways. compModDate=$(stat -f "%m" "$builtInfoPlistPath") for filename in * do modDate=$(stat -f "%m" "$filename") if [ "$modDate" -gt "$compModDate" ] then modifiedFilesExist=true; echo "found newly modified file: $filename" break fi done if $modifiedFilesExist then echo "A file is new, bumping version" #increment the build number buildNumber=$(/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" "$builtInfoPlistPath") echo "retrieved current build number: $buildNumber" buildNumber=$(($buildNumber + 1)) /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$builtInfoPlistPath" /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE" #compose the version number string versionString=$(/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "$builtInfoPlistPath") versionString+=" (" versionString+=$(/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" "$builtInfoPlistPath") versionString+=")" #write the version number string to the settings bundle #IMPORTANT: this assumes the version number is the second property in the settings bundle! /usr/libexec/PlistBuddy -c "Set :PreferenceSpecifiers:1:DefaultValue $versionString" "Settings.bundle/Root.plist" else echo "Version not incremented -- no newly modified files" fi 

我遇到过同样的问题。 要在Settings.bundle显示Title属性,还需要添加“Default Value”(可以为空)。

1)右键单击标题对象(在我的情况下它是项目0)并select添加行。

2)在创build的行的下拉列表中select“默认值”

在这里输入图像说明

3)在didFinishLaunchingWithOptions设置你想要显示的NSUserDefaults值,例如:

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setValue:@"0.0.1" forKey:@"appVersion"]; [defaults synchronize]; 

结果:

在这里输入图像说明

您可以将设置捆绑包同步到NSUserDefaults ,但奇怪的是,它不是一开始就做的。 您必须先将设置中的值检索到NSUserDefaults ,然后在NSUserDefaults中对这些值进行编辑,然后将其自动应用于设置包。

我引用了这个不错的文章 。

编辑:

为了您的情况只是为了保存您的版本,这样的事情会起作用。 (这个示例代码在某种程度上是矫枉过正的,但是应该更容易理解stream程)

 //Get the bundle file NSString *bPath = [[NSBundle mainBundle] bundlePath]; NSString *settingsPath = [bPath stringByAppendingPathComponent:@"Settings.bundle"]; NSString *plistFile = [settingsPath stringByAppendingPathComponent:@"Root.plist"]; //Get the Preferences Array from the dictionary NSDictionary *settingsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistFile]; NSArray *preferencesArray = [settingsDictionary objectForKey:@"PreferenceSpecifiers"]; //Save default value of "version_number" in preference to NSUserDefaults for(NSDictionary * item in preferencesArray) { if([[item objectForKey:@"key"] isEqualToString:@"version_number"]) { NSString * defaultValue = [item objectForKey:@"DefaultValue"]; [[NSUserDefaults standardUserDefaults] setObject:defaultValue forKey:@"version_number"]; [[NSUserDefaults standardUserDefaults] synchronize]; } } //Save your real version number to NSUserDefaults NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]; [[NSUserDefaults standardUserDefaults] setValue:version forKey:@"version_number"]; [[NSUserDefaults standardUserDefaults] synchronize]; 

这对我工作:

编辑Root.plist作为源代码:右键单击该文件,然后select打开为 – >源代码

为标题添加部分:

  <key>PreferenceSpecifiers</key> <array> <dict> <key>DefaultValue</key> <string>NoVersion</string> <key>Key</key> <string>Version</string> <key>Title</key> <string>Version</string> <key>Type</key> <string>PSTitleValueSpecifier</string> </dict> 

在 – (BOOL)应用程序中:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

添加这个:

 NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; [[NSUserDefaults standardUserDefaults] setObject:version forKey:@"Version"]; 

它将在应用程序第一次启动后生效。

另一个解决scheme,不写快捷的代码是:

1 /创build一个设置包

这将在您设备的设置中为您的应用创build一个新的部分

  • 右键单击您的项目名称 – >新build文件 – >设置包

在这里输入图像说明

2 /修改Root.plist

这将设置你想在你的应用程序设置中显示的内容 – 在新的设置包中,右键单击Root.plist – >打开为 – >源代码

  • 复制粘贴下面的代码:

     <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>PreferenceSpecifiers</key> <array> <dict> <key>Title</key> <string>About</string> <key>Type</key> <string>PSGroupSpecifier</string> </dict> <dict> <key>DefaultValue</key> <string></string> <key>Key</key> <string>version_preference</string> <key>Title</key> <string>Version</string> <key>Type</key> <string>PSTitleValueSpecifier</string> </dict> </array> <key>StringsTable</key> <string>Root</string> </dict> </plist> 

3 /创build一个运行脚本

这将始终从Info.plist中获取您的应用程序的版本,并将其显示在您的应用程序设置中

  • 导航到左侧面板上的项目目标,然后点击Build Phases
  • 点击“+”button,然后点击“新build脚本阶段”
  • 在“最新运行脚本”部分中,复制/粘贴以下内容:

     #Getting Current Version VERSIONNUM=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${PROJECT_DIR}/${INFOPLIST_FILE}") #Setting the version number in settings /usr/libexec/PlistBuddy -c "Set PreferenceSpecifiers:1:DefaultValue $VERSIONNUM" <YOUR-APP-NAME>/Settings.bundle/Root.plist 
  • 只需用您的应用程序名称replaceYOUR-APP-NAME

NSUserDefaults不会将任何值写入设置文件。 它只会将数据保存在用户默认的plist中。

要保存在另一个文件的东西,你将不得不写这个你自己的。 也许这将有助于:

iOS – 如何在plist中写入数据?