Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
        }
    }
}
like image 523
Tadej Avatar asked Aug 04 '17 12:08

Tadej


People also ask

How do I get OS version in xamarin?

Go To Xamarin Studio. Click New Solution—> select Multiplatform—>select App--> Choose single View App. Native (iOS,Android). Afterwards, click Next.

What is ContentPage content?

A ContentPage is a Page that is intended to fill the display. A ContentView is a View that can be contained within a Page or within another View . Both have a Content property that contains child content - either a single View , or more commonly a Layout containing multiple Views .

How can I get current page in xamarin?

You can get current open page. 1) if it's MainPage. -> var MainPage = App. Current.

What is master detail page?

A Page that manages two panes of information: A master page that presents data at a high level, and a detail page that displays low-level details about information in the master.


2 Answers

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;
like image 133
Steven Thewissen Avatar answered Sep 30 '22 18:09

Steven Thewissen


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 :

like image 34
JoeTomks Avatar answered Sep 30 '22 17:09

JoeTomks