MrBr MrBr -4 years ago 153
iOS Question

Simple title output in ios settings bundle

I just want to output the version number of my ios application in a settings file.

I understand that I have to add the settings file to the application folder.

When I build and run I can see the 4 settings which come with the standard settings-bundle.

In order to get a simple read-only string I change the second value to the following

enter image description here

In code (didFinishLaunchingWithOptions:) I call the following:

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


To my surprise nothing happens at all. I just see the Group element and the toogle switch and the slider but no title line. Anybody knows what I am missing?

Thank you very much!

Answer Source

Ok, I had this problem too. The solution (sort of) was to provide a default value field and give that a value. This is actually explicitly stated in the documentation -- Default Value is a required field for the Title property, so if you don't specify it the title won't show in the settings pane. Unfortunately, I can't seem to CHANGE the value once it's set, possibly also as designed -- the documentation also states that it's a read only property. The solution I'm going to try is to just explicitly put the version number in my Root.plist file each time I make a new build. SUPER not ideal, but will work, I think.

EDIT: Check out this post on updating version number in settings bundle

EDIT: Ok, I got this working (thanks to that post, above, and a little hacking around with bash scripts, which I had very little experience with.) Here's the script (which I just wrote inline in a 'Run Script' build phase):

#!/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"

... and that's it! Works like a charm! Hope that helps with your problem, because it solved mine. Only problem now is a slight discrepancy with the build number ...

EDIT: ... which I fixed with vakio's second comment on this post, which instead sets the path to the info.plist to the one which is already processed (before the Run Script phase!)

EDIT: Here's my more up-to-date version, which is in an external file and verifies that some source files have changed before incrementing the build number:

 #!/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 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download