首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ios设置捆绑包中的简单标题输出

ios设置捆绑包中的简单标题输出
EN

Stack Overflow用户
提问于 2012-10-17 19:08:20
回答 6查看 14.2K关注 0票数 17

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

我知道我必须将设置文件添加到应用程序文件夹中。

当我构建并运行时,我可以看到标准的settings-bundle附带的4个设置。

为了获得一个简单的只读字符串,我将第二个值更改为以下值

在代码(didFinishLaunchingWithOptions:)中,我调用以下代码:

代码语言:javascript
复制
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
[[NSUserDefaults standardUserDefaults] setValue:version forKey:@"version_number"];
[[NSUserDefaults standardUserDefaults] synchronize];

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

非常感谢!

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2012-11-01 02:24:13

好吧,我也有这个问题。解决方案(某种程度上)是提供一个缺省值字段,并给它一个值。这实际上是在文档中明确规定的--默认值是Title属性的必填字段,所以如果您不指定它,标题将不会显示在设置窗格中。不幸的是,一旦设置了值,我似乎就不能再更改它,可能也是按照设计的那样--文档还指出它是一个只读属性。我要尝试的解决方案是在每次创建新构建时显式地将版本号放在Root.plist文件中。超级不理想,但我认为会行得通。

编辑:检出this post on updating version number in settings bundle

编辑:好的,我把这篇文章写好了(感谢上面的那篇文章,还有一些关于bash脚本的小技巧,我对bash脚本几乎没有什么经验)。以下是脚本(我刚刚在“Run script”构建阶段内联编写了该脚本):

代码语言:javascript
复制
#!/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"

..。就是这样!就像一种护身符!希望这对你的问题有所帮助,因为它解决了我的问题。现在唯一的问题是内部版本号有一个细微的差异...

编辑:...我用vakio's second comment on this post修复了这个问题,它将info.plist的路径设置为已经处理过的路径(在运行脚本阶段之前!)

编辑:这是我的最新版本,它在一个外部文件中,在增加内部版本号之前验证一些源文件是否已经更改:

代码语言:javascript
复制
 #!/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 
票数 24
EN

Stack Overflow用户

发布于 2014-03-28 20:22:48

我也有同样的问题。要在Settings.bundle中显示Title属性,还需要添加"Default Value“(可以为空)。

1)右键单击Title对象(在我的示例中是Item 0),然后选择Add Row。

2)在已创建行的下拉列表中选择"Default Value“

3)在didFinishLaunchingWithOptions中,将您想要显示的值设置为NSUserDefaults,例如:

代码语言:javascript
复制
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:@"0.0.1" forKey:@"appVersion"];
[defaults synchronize];

结果:

票数 26
EN

Stack Overflow用户

发布于 2012-10-17 19:26:56

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

我引用了这个nice article

编辑:

对于你的案例来说,仅仅是为了保存你的版本,像这样的东西就可以工作了。(这个示例代码在某种程度上有点夸张,但应该更容易理解流程)

代码语言:javascript
复制
//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];    
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12932974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档