iOS Question Get latest app version number from the App Store

Alex_197

Well-Known Member
Licensed User
Longtime User
Hi all

Is it legal to get a latest app version by scrapping AppStore tml like this?

B4X:
 Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim wc As New WebClient
        Try

            Dim str As String, str2 As String
            Dim pos As Integer, pos2 As Integer


            str = wc.DownloadString(URLToAppStore)

            pos = str.IndexOf("l-column small-6 medium-12 whats-new__latest__version")
            pos2 = pos + 62

            str2 = str.Substring(pos2, 5).Trim

            Label3.Text = str2


        Catch ex As Exception
            Label3.Text = ex.Message
        Finally

        End Try
    End Sub

Is there any other way to get it?

I'm asking because my app on start checks if the version on iPhone matches to the latest version on the AppStore and if not redirects to the AppStore page for update. Currently my app connects to the office server where we manually update version number after each app update. The problem is that the app might be ready for sale at 3 AM, some iPhones will get the update automatically and users who work at night shift won't be able to continue because their app version will be bigger than the version number on the office server.
Thanks

Thanks.
 

JordiCP

Expert
Licensed User
Longtime User
To my understanding it should be legal, since you are accessing a public URL and parsing the results to get some info from a known position (however, just in case I'd search for this topic in the Apple publishing guidelines). The 'danger' with this specific URL is that, since it is not itself an API but an HTML page, if one day they happen to change the response format, you'll not be able to extract the info or even, whats worse, get incorrect results.

HERE they use this SWIFT code. I think it can be easily 'translated' using HttpJob and then parsing the JSON result. Only the app BundleID and country code are needed to build the URL string.
B4X:
guard let bundleId = Bundle.main.infoDictionary?["CFBundleIdentifier"] as? String else { return }
guard let url = URL(string: "https://itunes.apple.com/lookup?bundleId=\(bundleId)&country=br") else { return }
    let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
    if let data = data {
        do {
            let jsonObject = try JSONSerialization.jsonObject(with: data)
            guard let json = jsonObject as? [String: Any] else {
                print("The received that is not a Dictionary")
                return
            }
            let results = json["results"] as? [[String: Any]]
            let firstResult = results?.first
            let currentVersion = firstResult?["version"] as? String
            print("currentVersion: ", currentVersion)
        } catch let serializationError {
            print("Serialization Error: ", serializationError)
        }
    } else if let error = error {
        print("Error: ", error)
    } else if let response = response {
        print("Response: ", response)
    } else {
        print("Unknown error")
    }
}
task.resume()
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
Yes, but we aren't guaranteed that one day either html structure or the whole url wouldn't change. Is there any API that is more reliable?
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
The Swift code points to an URL which returns a JSON result, so it seems to respond to an API. i don't know where it is documented, didn't search for it. Anyhow, the response structure could be guessed from the swift code.
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
Thanks

Here is my VB.NET code

B4X:
    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim wc As New WebClient
        Try

            Dim str As String, str2 As String

            Dim url As String = "https://itunes.apple.com/lookup?bundleId=yourbundlename&country=us"

            str = wc.DownloadString(url)
            Label3.Text = str
            Dim json As JObject = JObject.Parse(str)
            str2 = json.SelectToken("results").First.SelectToken("version")
            Label3.Text &= "<hr/>" & str2

        Catch ex As Exception
            Label3.Text = ex.Message
        Finally

        End Try
    End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…