How to get the app version from ContentPage?
Asked Answered
S

3

9

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();
        }
    }
}
Simpson answered 4/8, 2017 at 12:10 Comment(0)
A
15

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;
Anfractuous answered 4/8, 2017 at 15:8 Comment(2)
Working like a charm. I just had to use constructors instead of properties. Don't know why it didn't work with properties (i added it to my question). Thanks! :)Simpson
Where exact must I put this code in the android and ios project ? And where in the shared code ? I am a complete newbie in xamarin so I don't understand how to use thisObject
G
6

If you don't want to use dependency services, you can just use the class VersionTracking.

The property VersionTracking.CurrentVersion will give you the Version you can set in your Android properties and your iOS info.plist.

This class is provided by Xamarin.Essentials and can give you a lot of informations. Please, check the documentation here for more informations.

Germicide answered 20/6, 2019 at 8:58 Comment(0)
R
4

Edit: listed incorrect nuget package, changes made below.

You should in theory be able to use something like the below inside the 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 use a plugin created by Mark Trinder called "Xam.Plugin.Version" which can be found on nuget1 and on GitHub2. Once it's installed into your forms & native projects it's simply called as so:

using Version.Plugin;

private void SomeMethod()
{
     MyLabel.Text = CrossVersion.Current.Version;
}

1 nuget package Here

2 Github Here :

Rangefinder answered 4/8, 2017 at 12:25 Comment(4)
I don't believe that NuGet has any such functionality in it. If it did I'd love to see a piece of code for it :)Anfractuous
This didn't work for me. "'Error: App.OnCreate()': no suitable method found to override"Simpson
@JedatKinports apologies I rushed posting that answer and cross referenced a few incorrect details, edit has been made.Rangefinder
@StevenThewissen Apologies Steven, you were right I cross referenced the wrong nuget package from one of our forms project. Updated the answer above with the correct one.Rangefinder

© 2022 - 2024 — McMap. All rights reserved.