Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check the .net maui latest app version?

Tags:

.net

version

maui

I'm new to .NET Maui. I would like to notify users who have an old version of my app that a new version is available on the store. I managed to do it when the user click on the Login Button with these few lines of code:


[RelayCommand]

async Task Login()
{

    var isLatest = await CrossLatestVersion.Current.IsUsingLatestVersion();

    if (!isLatest)
    {
        await Application.Current.MainPage.DisplayAlert("Update", 
              "New version available", "OK");
        await CrossLatestVersion.Current.OpenAppInStore();
    }
    else
    {...}
}

But I would like to notify the user before the LoginPage appears. I tried to do it in the method

protected override async void OnAppearing()

of the LoginPage but it doesn't work, it seems that the call

CrossLatestVersion.Current.IsUsingLatestVersion();

dies, without errors and otherwise unanswered...

Any suggestions? Thank you all in advance!!

like image 807
Davide Avatar asked Oct 30 '25 13:10

Davide


1 Answers

Inref to the above comment, here is the code snippet i use to get the latest version of App from the stores. It is the same technique used in the nugget package you are using. Try to get into the GitHub of the nugget you using, its similar.

public async Task<string> GetLatestVersionAvailableOnStores()
        {
            string remoteVersion = "";
            string bundleID = YOUR_BUNDLE_ID;
            bool IsAndroid = DeviceInfo.Current.Platform == DevicePlatform.Android;

            var url = IsAndroid ?
                "https://play.google.com/store/apps/details?id=" + bundleID :
                "http://itunes.apple.com/lookup?bundleId=" + bundleID;

            using (HttpClient httpClient = new HttpClient())
            {
                string raw = await httpClient.GetStringAsync(new Uri(url));

                if (IsAndroid)
                {
                    var versionMatch = Regex.Match(raw, @"\[\[""\d+.\d+.\d+""\]\]"); //look for pattern [["X.Y.Z"]]
                    if (versionMatch.Groups.Count == 1)
                    {
                        var versionMatchGroup = versionMatch.Groups[0];
                        if (versionMatchGroup.Success)
                            remoteVersion = versionMatch.Value.Replace("[", "").Replace("]", "").Replace("\"", "");
                    }
                }
                else
                {
                    JObject obj = JObject.Parse(raw);
                    if (obj != null)
                        remoteVersion = obj["results"]?[0]?["version"]?.ToString() ?? "9.9.9";
                }
            }

            return remoteVersion;
        }

You can have the Application version by using any static variable in your project. And then you can perform the comparison between both the strings. I used natural comparison to check which is the latest version.

iOS:

            SharedProjectClass.ApplicationVersion = NSBundle.MainBundle.InfoDictionary["CFBundlePublicVersionString"].ToString();

Android:

            SharedPRojectClass.ApplicationVersion = PackageManager.GetPackageInfo(PackageName, 0).VersionName;

Common Project:
    public static class SharedProjectCLass
    {
        /// <summary>
        /// The application version.
        /// </summary>
        public static string ApplicationVersion;
     }
async Task CheckVersion()
{
            var currentVersion = SharedProjectClass.ApplicationBuildNumber;

            var remoteVersion = await GetLatestVersionAvailableOnStores();

            // Function to natural compare the App version of format d.d.d
            Func<string, string, bool> CompareNaturalStringsOfAppVersion = (currentVersion, remoteVersion) =>
            {
                string[] components1 = currentVersion.Split('.');
                string[] components2 = remoteVersion.Split('.');

                for (int i = 0; i < components1.Length; i++)
                {
                    int value1 = Convert.ToInt32(components1[i]);
                    int value2 = Convert.ToInt32(components2[i]);

                    if (value1 < value2)
                    {                  
                        return true; // string1 is lower
                    }
                    else if (value1 > value2)
                    {
                        return false; // string2 is lower
                    }
                }

                return false; // both strings are equal
            };

            bool needToUpdateApp = CompareNaturalStringsOfAppVersion(currentVersion, remoteVersion);

            if (needToUpdateApp)
            {
                 //Show aleart pop-up to user saying new version is available.
            }
}
like image 119
I Am The Blu Avatar answered Nov 02 '25 03:11

I Am The Blu