Reputation: 1713
I have added Settings.bundle
to my app to add version and build numbers in app settings, and it is added properly. The issue I have is, that it is always getting default value only and after that it is not updating it based on the value from actual app version and build number (even after running the app).
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>StringsTable</key>
<string>Root</string>
<key>PreferenceSpecifiers</key>
<array>
<dict>
<key>DefaultValue</key>
<string>1.0</string>
<key>Key</key>
<string>current_version_number</string>
<key>Title</key>
<string>Version</string>
<key>Type</key>
<string>PSTitleValueSpecifier</string>
</dict>
</array>
</dict>
</plist>
I have created this custom class to update version and build number:
public static class SettingsBundleHelper
{
static string AppVersionKey = "current_version_number";
public static void SetVersionAndBuildNumber()
{
string version = NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleShortVersionString").ToString();
string build = NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleVersion").ToString();
NSUserDefaults.StandardUserDefaults.SetString(AppVersionKey, $"{version}({build})");
NSUserDefaults.StandardUserDefaults.Synchronize();
}
}
App Delegate:
public override void OnActivated(UIApplication application)
{
SettingsBundleHelper.SetVersionAndBuildNumber();
}
I followed this tutorial to implement this feature.
After this, version number in app settings is always 1.0
Another Question:
How to implement auto increment build number in Xamarin.iOS
app? Because I don't want to update info.plist
build number each time I create new ipa
.
Upvotes: 1
Views: 1623
Reputation: 1713
I have implemented the required by following this answer so I'm still using same Root.plist
file which mentioned in my question, and then implemented the following:
BuildNumberIncremental.sh
Before Build
-> in Command field add sh BuildNumberIncremental.sh
.BuildNumberIncremental.sh
Version_Number=$(/usr/libexec/PlistBuddy ${ProjectDir}/info.plist -c "print :CFBundleShortVersionString")
Build_Number=$(($(/usr/libexec/PlistBuddy ${ProjectDir}/info.plist -c "print :CFBundleVersion") + 1))
Full_Version="$Version_Number(b$((Build_Number + 1)))"
/usr/libexec/PlistBuddy ${ProjectDir}/info.plist -c "set :CFBundleVersion $Build_Number"
/usr/libexec/PlistBuddy ${ProjectDir}/Settings.bundle/Root.plist -c "set PreferenceSpecifiers:0:DefaultValue $Full_Version"
${ProjectDir}
with your Xamarin.iOS directory.Now each time I build the app, it will save new value in Root.plist i.e 1.5(b6)
Upvotes: 1
Reputation: 6643
I think you are in a wrong way. We can just access the value from Settings.bundle
but we can't change it at run time, since the settings bundle resides inside your app's bundle.
As you said, you have already known the NSUserDefaults
and Settings.bundle
always keep in sync. You can get the value from NSUserDefaults
through the same Key
. But what you should notice is that the NSUserDefaults
will get the value after we have changed the value of Settings manually.
It means you will get null
at the first launched time. This is why your tutorial calls setVersionAndBuildNumber()
to set the NSUserDefaults
's value from bundle. Then when Settings changes NSUserDefaults
changes too.
In your case, you set a type PSTitleValueSpecifier
, so this value can't be changed in Settings. Also we can't and shouldn't modify it dynamically at run time. Here is a sample about how to use Settings Bundle on Xamarin, you can refer to it for more details: https://github.com/xamarin/ios-samples/tree/master/AppPrefs
How to implement auto increment build number in Xamarin.iOS app?
As we said above, the file embed in our project can't be modified dynamically at run time. You should update it manually every time you want to archive a new ipa.
Upvotes: 1