0

I am reading this documentation about app center in-app updates. I want to try it so that every time there is a release of my app I don't need to uninstall my app and install the new release everytime. There is a sample code in the documentation but I don't know where to put it or how it works the documentation is not clear. The code below is the sample code from the documentation. My problem is how can I implement in-app updates for my app?

https://learn.microsoft.com/en-us/appcenter/sdk/distribute/xamarin

bool OnReleaseAvailable(ReleaseDetails releaseDetails)
{
    // Look at releaseDetails public properties to get version information, release notes text or release notes URL
    string versionName = releaseDetails.ShortVersion;
    string versionCodeOrBuildNumber = releaseDetails.Version;
    string releaseNotes = releaseDetails.ReleaseNotes;
    Uri releaseNotesUrl = releaseDetails.ReleaseNotesUrl;

    // custom dialog
    var title = "Version " + versionName + " available!";
    Task answer;

    // On mandatory update, user cannot postpone
    if (releaseDetails.MandatoryUpdate)
    {
        answer = Current.MainPage.DisplayAlert(title, releaseNotes, "Download and Install");
    }
    else
    {
        answer = Current.MainPage.DisplayAlert(title, releaseNotes, "Download and Install", "Maybe tomorrow...");
    }
    answer.ContinueWith((task) =>
    {
        // If mandatory or if answer was positive
        if (releaseDetails.MandatoryUpdate || (task as Task<bool>).Result)
        {
            // Notify SDK that user selected update
            Distribute.NotifyUpdateAction(UpdateAction.Update);
        }
        else
        {
            // Notify SDK that user selected postpone (for 1 day)
            // Note that this method call is ignored by the SDK if the update is mandatory
            Distribute.NotifyUpdateAction(UpdateAction.Postpone);
        }
    });

    // Return true if you are using your own dialog, false otherwise
    return true;
}
  • 1
    That code sample you refer to is just to customize the dialog, if you want the default dialog you have nothing special to do and don't need to override OnReleaseAvailable. There is no need to uninstall anything either way. – Guillaume Perrot Dec 11 '18 at 00:22

1 Answers1

1

Android:

You set the callback to your OnReleaseAvailable method via Distribute.ReleaseAvailable

Open MainActivity.cs and add the Start() call inside the OnCreate() method

Distribute.ReleaseAvailable = OnReleaseAvailable;
AppCenter.Start(...);

iOS:

Open your AppDelegate.cs and add the Start() call inside the FinishedLaunching() method

Distribute.ReleaseAvailable = OnReleaseAvailable;
AppCenter.Start(...);

re: https://learn.microsoft.com/en-us/appcenter/sdk/distribute/xamarin#2-start-app-center-distribute

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • How about for Xamarin Forms? I inserted the AppCenter.Start(...); in my App.xaml.cs in my xamarin forms –  Dec 11 '18 at 00:12
  • That is for Forms, you do not want to allow a UI popup in app's ui till it is ready. – SushiHangover Dec 11 '18 at 00:14
  • 1
    Do I need to put AppCenter.Start(...); and Distribute.ReleaseAvailable = OnReleaseAvailable; on each of my project I mean in my Android, IOS, UWP and Xamarin Forms project?? –  Dec 11 '18 at 00:15
  • 1
    Yes, in each of your projects. You can try it in App.cs (maybe they fixed the issues of having the popup crash the app as it was not init'd yet.) – SushiHangover Dec 11 '18 at 00:15
  • And when I put Distribute.ReleaseAvailable = OnReleaseAvailable; it will automatically update the app whenever there is a new release? –  Dec 11 '18 at 00:17
  • It will prompt the user. – SushiHangover Dec 11 '18 at 00:17
  • So they dont need to uninstall the app in order to update the app? And do I need to set anything else in my projects? –  Dec 11 '18 at 00:19
  • Correct, that is why this method does NOT work with apps from the App Stores as that would be an app signing violation to try to change the code within an existing app. https://learn.microsoft.com/en-us/appcenter/sdk/distribute/xamarin#how-do-in-app-updates-work – SushiHangover Dec 11 '18 at 00:22
  • there is an error on Distribute.ReleaseAvailable = OnReleaseAvailable; whe I put it in MainActivity.cs –  Dec 11 '18 at 00:30
  • Does not exist in current context –  Dec 11 '18 at 00:38
  • Depends upon where you put OnReleaseAvailable, if it is the Application subclass, make it public and get the current Application and cast it as App (assuming that is your subclass) or as I said, try it in your App.cs and see if they fixed the timing issue on the app initization that caused the app crashes. I am a couple dot versions behind as we had to pull it from a customer's app due to security issues. – SushiHangover Dec 11 '18 at 00:43
  • Can you help me do you have any desk? can you remote ng pc to see what I am doing wrong? –  Dec 11 '18 at 00:49