Thursday, 10 May 2018

How to get App Version Number and Build Number in Xamarin.Forms

Introduction:

Version and the build numbers work together to uniquely identify a particular App Store submission for an app. The conventions for how these numbers work together are verified by automatic processes when you submit your app to the App Store or Play Store, so understanding how these numbers work and how they are intended to be used will help you save time when submitting your app. 

Requirements:
  1. This article's source code is prepared by using Visual Studio. And it is better to install the latest Visual Studio updates from here.
  2. This article is prepared on a MAC machine
  3. This sample project is Xamarin.Forms PCL project.
  4. This sample app is targeted for Android, iOS. And, tested for Android & iOS.
Description:

For each new version of your app, you will provide a version number to differentiate it from previous versions. The version number works like a name for each release of your app. For example, version 1.0.0 may name the first release, version 2.0.1 will name the second, and so on. When submitting a new release of your app to the App Store or Play Store, it is normal to have some false starts. You may forget an icon in one build, or perhaps there is a problem in another build. As a result, you may produce many builds during the process of submitting a new release of your app to the App Store  or Play Store. Because these builds will be for the same release of your app, they will all have the same version number. But, each of these builds must have a unique build number associated with it so it can be differentiated from the other builds you have submitted for the release. The collection of all of the builds submitted for a particular version is referred to as the 'release train' for that version.

iOS:
In iOS the version number and the build number are also displayed in your app's Info.plist file.

Info.plist:



Android:
In android we can see the version and build numbers in Manifest file.
AndroidManifest.xml



Let's start to get app Version and Build numbers through programming.

1) Xamarin.Forms

Portable Class Library(PCL):

Step1:
Create an interface, IAppVersionAndBuild.cs, with the methods declaration of GetVersionNumber and GetBuildNumber.

IAppVersionAndBuild.cs:
  1. namespace VersionAndBuildNumber.DependencyServices    
  2. {    
  3.     public interface IAppVersionAndBuild    
  4.     {    
  5.         string GetVersionNumber();    
  6.         string GetBuildNumber();    
  7.     }    
  8. }  

Step 2: 
Create your own Xaml page named VersionAndBuildNumberPage.xaml.

VersionAndBuildNumberPage.xaml

  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" BackgroundColor="#533F95" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:VersionAndBuildNumber" x:Class="VersionAndBuildNumber.VersionAndBuildNumberPage">    
  3.     <StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" Spacing="40">    
  4.     <StackLayout >    
  5.         <Label Text="Version Number:" TextColor="White" FontAttributes="Bold" FontSize="25"/>    
  6.         <Label x:Name="lblVersionNumber" TextColor="White" FontAttributes="Bold" FontSize="25"/>    
  7.     </StackLayout>    
  8.     <StackLayout>    
  9.         <Label Text="Build Number:" FontSize="25" TextColor="White" FontAttributes="Bold"/>    
  10.         <Label x:Name="lblBuildNumber" TextColor="White" FontAttributes="Bold" FontSize="25"/>    
  11.     </StackLayout>    
  12.     </StackLayout>    
  13. </ContentPage>    

Call to DependencyService

Now calling dependency service in the code behind for getting Version and Build numbers, also assinging those values to the lables. 

  1. DependencyService.Get<IAppVersionAndBuild>().GetVersionNumber();           

  1. DependencyService.Get<IAppVersionAndBuild>().GetBuildNumber();  

VersionAndBuildNumberPage.xaml.cs
  1. using VersionAndBuildNumber.DependencyServices;    
  2. using Xamarin.Forms;    
  3.     
  4. namespace VersionAndBuildNumber    
  5. {    
  6.     public partial class VersionAndBuildNumberPage : ContentPage    
  7.     {    
  8.         public VersionAndBuildNumberPage()    
  9.         {    
  10.             InitializeComponent();    
  11.     
  12.             lblVersionNumber.Text = DependencyService.Get<IAppVersionAndBuild>().GetVersionNumber();    
  13.             lblBuildNumber.Text = DependencyService.Get<IAppVersionAndBuild>().GetBuildNumber();    
  14.         }    
  15.     }    
  16. }    

Xamarin.Android:
Create a class, VersionAndBuild_Android, and implement the IAppVersionAndBuild methods like below.

VersionAndBuild_Android.cs

  1. using Android.Content.PM;    
  2. using VersionAndBuildNumber.DependencyServices;    
  3. using VersionAndBuildNumber.Droid.DependencyServices;    
  4. using Xamarin.Forms;    
  5.     
  6. [assembly: Dependency(typeof(VersionAndBuild_Android))]    
  7. namespace VersionAndBuildNumber.Droid.DependencyServices    
  8. {    
  9.     public class VersionAndBuild_Android : IAppVersionAndBuild    
  10.     {    
  11.         PackageInfo _appInfo;    
  12.     
  13.         public VersionAndBuild_Android()    
  14.         {    
  15.             var context = Android.App.Application.Context;    
  16.             _appInfo = context.PackageManager.GetPackageInfo(context.PackageName, 0);    
  17.         }    
  18.     
  19.         public string GetVersionNumber()    
  20.         {    
  21.             return _appInfo.VersionName;    
  22.         }    
  23.     
  24.         public string GetBuildNumber()    
  25.         {    
  26.             return _appInfo.VersionCode.ToString();    
  27.         }    
  28.     }    
  29. }  
Note:
PackageManager Class for retrieving various kinds of information related to the application packages that are currently installed on the device.

Xamarin.iOS:
Create a class, VersionAndBuild_iOS, and implement the IAppVersionAndBuild methods like below.

VersionAndBuild_iOS.cs
  1. using Foundation;    
  2. using VersionAndBuildNumber.DependencyServices;    
  3. using VersionAndBuildNumber.iOS.DependencyServices;    
  4. using Xamarin.Forms;    
  5.     
  6. [assembly: Dependency(typeof(VersionAndBuild_iOS))]      
  7. namespace VersionAndBuildNumber.iOS.DependencyServices    
  8. {    
  9.     public class VersionAndBuild_iOS : IAppVersionAndBuild    
  10.     {    
  11.         public string GetVersionNumber()    
  12.         {    
  13.             //var VersionNumber = NSBundle.MainBundle.InfoDictionary.ValueForKey(new NSString("CFBundleShortVersionString")).ToString();    
  14.             return NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleShortVersionString").ToString();    
  15.         }    
  16.     
  17.         public string GetBuildNumber()    
  18.         {    
  19.             //var BuildNumber = NSBundle.MainBundle.InfoDictionary.ValueForKey(new NSString("CFBundleVersion")).ToString();    
  20.             return NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleVersion").ToString();    
  21.         }    
  22.     }    
  23. }   
Note:
CFBundleShortVersionString, CFBundleVersion these or the  Core Foundation Keys in iOS. The Core Foundation framework provides the underlying infrastructure for bundles, including the code used at runtime to load bundles and parse their structure. As a result, many of the keys recognized by this framework are fundamental to the definition of bundles themselves and are instrumental in determining the contents of a bundle.

2) Xamarin.Android

  1. var context = Android.App.Application.Context;     
  2. var VersionNumber = context.PackageManager.GetPackageInfo(context.PackageName, PackageInfoFlags.MetaData).VersionName;    


  1. var BuildNumber = context.PackageManager.GetPackageInfo(context.PackageName, PackageInfoFaags.MetaData).VersionCode.ToString();   


3) Xamarin.iOS

  1. var VersionNumber = NSBundle.MainBundle.InfoDictionary.ValueForKey(new NSString("CFBundleShortVersionString")).ToString();    


  1. var BuildNumber = NSBundle.MainBundle.InfoDictionary.ValueForKey(new NSString("CFBundleVersion")).ToString();    

Output:

Please download  source code from below


No comments:

Post a Comment