0

I want to check if there is an update on App Store for the app. To do so, I need to get the last version of the app in App Store.

For android (it's working fine):

actual class AppUpdateManager(private val context: Context) {
    actual suspend fun checkForUpdate(): UpdateStatus  = suspendCancellableCoroutine { cont ->
        val appUpdateManager = AppUpdateManagerFactory.create(context)
        val task = appUpdateManager.appUpdateInfo

        task.addOnSuccessListener { info ->
            if (info.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE) {
                cont.resume(UpdateStatus.UpdateAvailable)
            } else {
                cont.resume(UpdateStatus.NoUpdate)
            }
        }

        task.addOnFailureListener {
            cont.resume(UpdateStatus.Error(it.message ?: "Unknown error"))
        }
    }
}

For IOS, I have this but couldn't find the right way to do it:

actual class AppUpdateManager {
    actual suspend fun checkForUpdate(): UpdateStatus {
        val currentVersion =
            NSBundle.mainBundle.infoDictionary?.get("CFBundleShortVersionString") as? String
        val latestVersion = fetchLatestAppStoreVersion() // I need to implement this

        return if (latestVersion != null && latestVersion > currentVersion.orEmpty()) {
            UpdateStatus.UpdateAvailable
        } else {
            UpdateStatus.NoUpdate
        }
    }
    private fun fetchLatestAppStoreVersion(): String? {
        // Call iTunes Lookup API via NSURLSession (or use ktor client in iosMain)
        return null // Placeholder
    }`

2 Answers 2

0

You can GET the current app version info from the iTunes API:

http://itunes.apple.com/lookup?bundleId=com.example.xyz

where com.example.xyz is your bundleId. The response will an object like the following:

{
  "resultCount": 1,
  "results": [
    {
      "version": "X.Y.Z"
      /* ... other fields ... */
    }
  ]
}

where "X.Y.Z" is the most recent CFBundleShortVersionString.

I am not a Kotlin expert, so I can't advise on how to make/process this HTTP request. It seems like Ktor is an option, or there's an example using URLSession on iOS.

Sign up to request clarification or add additional context in comments.

Comments

0

Since this is a KMP project, I highly recommend you use Ktor client, but I'm not sure how familiar you are with that, so I'll have to assume you've got an idea about it.
Also, based on the function signature, it seems like you're already in iosMain.

In that case, your function could look something like this:

private suspend fun fetchLatestAppStoreVersion(httpClient: HttpClient): String? {
  // This should get your bundle id and do the lookup
 val bundleId = NSBundle.mainBundle.bundleIdentifier ?: return null
  val url = "https://itunes.apple.com/lookup?bundleId=$bundleId"

  return try {
    val response: HttpResponse = httpClient.get(url)
    // Parse "response" and return the version from the body.
    // Or deserialize your custom response data class if you have ContentNegotiation: https://ktor.io/docs/client-serialization.html
  } catch (e: Exception) {
    // network error / parsing error
    null
  }
}

Ideally, you should have a convenient network or repository layer that performs your network requests for you, and you'll be passing/injecting that instead of HttpClient directly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.