How do I get the version of the app from ContentPage?
I tried this: iPhone MonoTouch - get package version
NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleVersion").ToString();
But it didn't work. Since NSBundle
it cannot be found.
How can I get the app version (iOS and Android) from ContentPage?
The code I ended up with (thanks to Steven Thewissen):
PCL (generic code)
using System;
namespace MyApp.Interfaces
{
public interface IApplicationVersion
{
string ApplicationsPublicVersion { get; set; }
string ApplicationsPrivateVersion { get; set; }
}
}
Android
using System;
using MyApp.Droid.Helpers;
using MyApp.Interfaces;
using Xamarin.Forms;
[assembly: Dependency(typeof(ApplicationVersion))]
namespace MyApp.Droid.Helpers
{
public class ApplicationVersion : IApplicationVersion
{
public string ApplicationsPublicVersion { get; set; }
public string ApplicationsPrivateVersion { get; set; }
public ApplicationVersion()
{
var context = Android.App.Application.Context;
var info = context.PackageManager.GetPackageInfo(context.PackageName, 0);
ApplicationsPublicVersion = info.VersionName;
ApplicationsPrivateVersion = info.VersionCode.ToString();
}
}
}
Ios
using System;
using MyApp.Interfaces;
using MyApp.iOS.Helpers;
using Foundation;
using Xamarin.Forms;
[assembly: Dependency(typeof(ApplicationVersion))]
namespace MyApp.iOS.Helpers
{
public class ApplicationVersion : IApplicationVersion
{
public string ApplicationsPublicVersion { get; set; }
public string ApplicationsPrivateVersion { get; set; }
public ApplicationVersion()
{
ApplicationsPublicVersion = NSBundle.MainBundle.InfoDictionary[new NSString("CFBundleShortVersionString")].ToString();
ApplicationsPrivateVersion = NSBundle.MainBundle.InfoDictionary[new NSString("CFBundleVersion")].ToString();
}
}
}
You can do this by injecting a dependency service. First, you define the interface in your generic code:
namespace MyApp
{
public interface IAppVersionProvider
{
string AppVersion { get; }
}
}
In every platform project, you implement an interface.
IOS
[assembly: Dependency(typeof(AppVersionProvider))]
namespace MyApp.iOS
{
public class AppVersionProvider : IAppVersionProvider
{
public string AppVersion => NSBundle.MainBundle.InfoDictionary[new NSString("CFBundleVersion")].ToString();
}
}
Android
[assembly: Dependency(typeof(AppVersionProvider))]
namespace MyApp.Droid
{
public class AppVersionProvider : IAppVersionProvider
{
public string AppVersion
{
get
{
var context = Android.App.Application.Context;
var info = context.PackageManager.GetPackageInfo(context.PackageName, 0);
return $"{info.VersionName}.{info.VersionCode.ToString()}";
}
}
}
}
Then you can get the version number from the generic code via:
var version = DependencyService.Get<IAppVersionProvider>();
var versionString = version.AppVersion;
Edit: Wrong nuget package listed below.
In theory, you should be able to use something like below inside OnStart (); method of your App.cs in your forms project.
Context context = this.ApplicationContext;
SupportFunctions.Version = context.PackageManager.GetPackageInfo(context.PackageName, 0).VersionName;
However, we are using a plugin created by Mark Trinder called "Xam.Plugin.Version" which can be found on nuget 1 and on GitHub 2 . Once it's installed in your forms and your own projects, it's simply called like this:
using Version.Plugin;
private void SomeMethod()
{
MyLabel.Text = CrossVersion.Current.Version;
}
1 nuget package Here
2 Github Here :
If you don't want to use dependency services, you can just use the class VersionTracking
. The property VersionTracking.CurrentVersion
will give you a version that you can install on your properties
Android and your info.plist
iOS.