0

I'm new to unity. I want to clear all localstorage and cache if the app is installed or updated. So how can I check is app is installed or getting updated. Pervious data would lead to app crash down.

I know how to clear data manually but I want to do it through app.

I know how to clear localstorage. PlayerPrefs.DeleteAll(), How to check is app getting updated or reinstalled.

Avi
  • 1,424
  • 1
  • 11
  • 32

1 Answers1

0

Updating

Well for at least checking if you updated the App you could store and compare the Application.version

This function returns the current version of the Application. This is read-only. To set the version number in Unity, go to EditProject SettingsPlayer and open the Other Settings tab.

public class VersionCheck : MonoBehaviour
{
    private void Awake()
    {
        var version = PlayerPrefs.GetString("Version", string.Empty);

        if (string.IsNullOrWhiteSpace(version))
        {
            // Probably not more to do since there is no stored data apparently
            // Just to be sure you could still do
            PlayerPrefs.DeleteAll();

            // => THIS IS THE FIRST RUN
            PlayerPrefs.SetString("Version", Application.version);
        }
        else 
        {
            if(version != Application.version)
            {
                // => THIS IS A VERSION MISMATCH -> UPDATED
                PlayerPrefs.DeleteAll();

                PlayerPrefs.SetString("Version", Application.version);
            }
            // else
            //{
            //     // Otherwise it could either mean you re-installed the same version 
            //     // or just re-started the app -> There should be no difference between these two in behavior of your app
            //}
        }
    }
}

Re-Installing

For a re-install: Would it matter? Actually if you install the same version then there should be no difference between running the again installed code or running the previously existing one again.

So I would suggest to rather add an option for the user to clear the data if it is needed.

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Can you help me, how to ask for a force update, If I push new build in play store or app store – Avi Jul 03 '20 at 06:49
  • I think [this thread for Android](https://stackoverflow.com/questions/19244305/force-update-of-an-android-app-when-a-new-version-is-available) and [this one for apple](https://stackoverflow.com/questions/2221436/can-i-force-an-iphone-user-to-upgrade-an-application) cover that way better than I could possibly explain that (especially since I have no experience with the PlayStore nor the AppStore) – derHugo Jul 03 '20 at 06:58