Forum Xamarin.Forms

Add Name, Version and Build properties to App class

rogiheerogihee NLMember ✭✭✭

Summary

Add a way to access your app's basic information (name, version, build) in a cross platform way. Depending on what is filled in the manifest or info.plist the results can differ per platform, but no service or plugin is required anymore to access the information.

API Changes

Extend the App class with the following properties Name (string), Version (string), Build (string).

Android implementation:

  • Version -> Application.Context.PackageManager.GetPackageInfo(Application.Context.PackageName, PackageInfoFlags.MetaData).VersionName
  • Build -> Application.Context.PackageManager.GetPackageInfo(Application.Context.PackageName, PackageInfoFlags.MetaData).VersionCode
  • Name -> use ApplicationInfo.LoadLabel(PackageManager)

iOS implementation:

  • Version -> CFBundleShortVersionString
  • Build -> CFBundleVersion
  • Name -> CFBundleName

UWP:
Package.Current.Id (?)

Intended Use Case

Typically, version information is displayed to a user somewhere in an about section, or used for (custom) logging purposes.

Tagged:

Open · Last Updated

Posts

  • DavidDancyDavidDancy AUMember ✭✭✭✭

    This would be very handy. I like the idea.

  • NickKovalskyNickKovalsky USMember ✭✭✭

    Handy indeed.

  • RaphaelSchindlerRaphaelSchindler USMember ✭✭✭

    UWP would be

    * Version = $"{Windows.ApplicationModel.Package.Id.Major}.{Windows.ApplicationModel.Package.Id.Minor}"
    * Build = Windows.ApplicationModel.Package.Id.Build
    * Name = Windows.ApplicationModel.Package.Id.Name
    
  • MichaelRumplerMichaelRumpler ATMember ✭✭✭✭✭

    That would conflict with the Name and Version properties in my App class. ;-)

    I use my own Version class for Version and OSVersion. That class makes comparisons easy. This way I can write something like:

    if(App.OSVersion >= (Version)"5.0")
    

    Where

    (Version)"5.0" == (Version)"5"
    (Version)"5.0" == (Version)"5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0"
    (Version)"5.0" <  (Version)"5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1"
    

    You may want to use (something like) this too.

    To initialize it, just cast a string to Version.

    Android:

    App.Name = ((global::Android.App.Activity)application).Title;
    App.Version = (Version)application.PackageManager.GetPackageInfo(application.PackageName, 0).VersionName;
    App.OSVersion = (Version)global::Android.OS.Build.VERSION.Release;
    

    iOS:

    App.Name = NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleDisplayName").ToString();
    App.Version = (Version)NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleShortVersionString").ToString();
    App.OSVersion = (Version)UIKit.UIDevice.CurrentDevice.SystemVersion;
    

    The Xamarin.Forms version would also come in handy (probably not in App).

  • DevaPalanisamyDevaPalanisamy GBUniversity ✭✭

    +1 :)

  • rogiheerogihee NLMember ✭✭✭

    @MichaelRumpler : nice variation! I was thinking about something like this too, but I was not sure that each platform would require a "parseable" int version.

    On iOS the CF*Bundle stuff is at most 3 ints with a dot. On Android, VersionCode is a single int up to 2100000000, but VersionName is completely free AFAIK. UWP dunno.

    Also, your Version would collide with the standard .Net Version code, which might confuse people coming from a WPF background for example. Hence I opted for the simplest approach: strings.

  • NMackayNMackay GBInsider, University mod
    edited May 2017

    In UWP I'm doing this to get the info currently.

        internal class HandsetSupport : IHandset
        {
    
            public string GetAppVersion()
            {
                var package = Package.Current;
                var packageId = package.Id;
                var version = packageId.Version;
                return $"{version.Major}.{version.Minor}.{version.Build}.{version.Revision}";
            }
    
            public string GetAppTitle()
            {
                return Package.Current.DisplayName;
            }
    
            public string GetAppCopyright()
            {
                return Package.Current.PublisherDisplayName;
            }
    
Sign In or Register to comment.