Forum Xamarin.Android

Xamarin.Android How to Get Google Play Store app version number using Dcsoup Nuget Plugin?

Cdn_EuroCdn_Euro Member ✭✭✭

I am trying to get the latest version number of my store app in order to notify user for updates if they are using an older version.

This is my code so far but its obviously just retrieving the div containing the text "Version Number". How do I get the actual version number (in this case 1.1) referring to the attached screenshot of the DOM tree?

Is this a good way to do it, or is there any other way to retrieve the latest store version of the app?

public static string GetAndroidStoreAppVersion()
        {
            string androidStoreAppVersion = null;

            try
            {
                using (var client = new HttpClient())
                {
                    var doc = client.GetAsync("https://play.google.com/store/apps/details?id=" + AppInfo.PackageName + "&hl=en_CA").Result.Parse();
                    var versionElement = doc.Select("div:containsOwn(Current Version)");
                    androidStoreAppVersion = versionElement.Text;
                }
            }
            catch (Exception ex)
            {
                // do something
                Console.WriteLine(ex.Message);
            }

            return androidStoreAppVersion;
        }

Answers

  • M_Shah04M_Shah04 USMember ✭✭✭

    Try this out, it may help you : https://github.com/edsnider/LatestVersionPlugin
    Hope this may solve your issue.

  • Cdn_EuroCdn_Euro Member ✭✭✭

    @M_Shah04 Would like to know how to do it without using a plug in. What if the plug in is not supported anymore? Then my app would break. Is this way of checking the store website version the common way its usually done? I can get the div where the "Version Number" text is contained but the actual "1.1" resides in a Span a couple levels down. Not sure how to retrieve it in code. As a last resort I would try to use that NugGet plugin. Thank you for the suggestion.

  • M_Shah04M_Shah04 USMember ✭✭✭

    @Cdn_Euro said:
    @M_Shah04 Would like to know how to do it without using a plug in. What if the plug in is not supported anymore? Then my app would break. Is this way of checking the store website version the common way its usually done? I can get the div where the "Version Number" text is contained but the actual "1.1" resides in a Span a couple levels down. Not sure how to retrieve it in code. As a last resort I would try to use that NugGet plugin. Thank you for the suggestion.

    I think the approach you are doing can also break in future due to any change done in google play store site, tried different ways but not able to get it done, Kindly post your solution as answer if its working proper, and Tag me so that I can get that solution.

  • Cdn_EuroCdn_Euro Member ✭✭✭
    edited November 2018

    @M_Shah04 You're right that approach could break too.But, I could maintain the code and adapt it to changes on the website. For sure when I solve this will share the solution.

  • Cdn_EuroCdn_Euro Member ✭✭✭
    edited November 2018

    @M_Shah04 Here is the solution as promised:

    Notes: This is valid for the current placement of the version information within the Apple and Google Play store websites
    respectively.
    Also using** Xarmarin Essentials** NuGet plug in when calling **AppInfo.***
    Using Dcsoup NuGet Plugin as well for navigating the Elements.
    The photo is referring to the android version information location oh the google play store URL.

    public class AppVersionServices
        {
    
            public static string GetAndroidStoreAppVersion()
            {
                string androidStoreAppVersion = null;
    
                try
                {
                    using (var client = new HttpClient())
                    {
                        var doc = client.GetAsync("https://play.google.com/store/apps/details?id=" + AppInfo.PackageName + "&hl=en_CA").Result.Parse();
                        var versionElement = doc.Select("div:containsOwn(Current Version)");
    
                        androidStoreAppVersion = versionElement.Text;
    
                        Element headElement = versionElement[0];
                        Elements siblingsOfHead = headElement.SiblingElements;
                        Element contentElement = siblingsOfHead.First;
                        Elements childrenOfContentElement = contentElement.Children;
                        Element childOfContentElement = childrenOfContentElement.First;
                        Elements childrenOfChildren = childOfContentElement.Children;
                        Element childOfChild = childrenOfChildren.First;
    
                        androidStoreAppVersion = childOfChild.Text;
                    }
                }
                catch (Exception ex)
                {
                    // do something
                    Console.WriteLine(ex.Message);
                }
    
                return androidStoreAppVersion;
            }
    
            public static string GetIosStoreAppVersion()
            {
                string iOsStoreAppVersion = null;
    
                string bundleId = AppInfo.PackageName;
    
                string url = "http://itunes.apple.com/lookup?bundleId=" + bundleId;
    
    
                try
                {
                    using (var webClient = new System.Net.WebClient())
                    {
                        string jsonString = webClient.DownloadString(string.Format(url));
    
                        var lookup = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);
    
    
                        if (lookup != null
                            && lookup.Count >= 1
                            && lookup["resultCount"] != null
                            && Convert.ToInt32(lookup["resultCount"].ToString()) > 0)
                        {
    
                            var results = JsonConvert.DeserializeObject<List<object>>(lookup[@"results"].ToString());
    
    
                            if (results != null && results.Count > 0)
                            {
                                var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(results[0].ToString());
                                iOsStoreAppVersion = values.ContainsKey("version") ? values["version"].ToString() : string.Empty;
    
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    // do something
                    Console.WriteLine(ex.Message);
                }
    
                return iOsStoreAppVersion;
            }
    
        }
    
  • M_Shah04M_Shah04 USMember ✭✭✭

    @Cdn_Euro Glad that you got your solution, Thanks for sharing it, Keep it up! :)

  • swap.5724swap.5724 USMember

    Thank you. It worked.

  • BharathiBogaBharathiBoga USMember ✭✭

    @cndfrgk ,Hi your solution worked for android. but not in ios. when i am trying to call i am getting the result as

    {
    "resultCount":0,
    "results": []
    }
    can you please help me out from this

  • DeepakDYDeepakDY INMember ✭✭✭

    Hello @Cdn_Euro

    I used your code for get current version of play store's beta version application.

    but i am getting some error.

    using System.Net.Http;
    
      using (var client = new HttpClient())
                    {
                        var doc = client.GetAsync("https://play.google.com/store/apps/details?id=" + "com.wikiteksystems.dtool" + "&hl=en_US").Result.Parse();
                    }
    

    in this line i am facing some issues
    HttpResponseMessage does not containt a definition for parse

    any idea about this

Sign In or Register to comment.