Search code examples
c#xamarin.iosxamarin.androidxamarin.formsportable-class-library

How to get the app version from ContentPage?


I tried this: iPhone MonoTouch - Get Version of Bundle

NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleVersion").ToString();

But this didn't work. As NSBundle can't be found.

How can I get the app version (iOS and Android) from ContentPage?

The code which i ended up with (thanks to Steven Thewissen):

PCL (shared 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();
        }
    }
}

Solution

  • You can do this by implementing a Dependency Service. First you define an interface in your shared code:

    namespace MyApp
    {
        public interface IAppVersionProvider
        {
            string AppVersion { get; }
        }
    }
    

    In each platform project you then implement the 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()}";
                }
            }
        }
    }
    

    You can then retrieve the version number from shared code through:

    var version = DependencyService.Get<IAppVersionProvider>();
    var versionString = version.AppVersion;