Skip to content

Instantly share code, notes, and snippets.

@sjehutch
Created December 1, 2022 14:45
Show Gist options
  • Save sjehutch/138e7d121d4e6747d5d8c58d6e0fcdc7 to your computer and use it in GitHub Desktop.
Save sjehutch/138e7d121d4e6747d5d8c58d6e0fcdc7 to your computer and use it in GitHub Desktop.
Check for app update Xamarin Prism
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using HOApp.Core;
using HOApp.Core.Localization;
using Newtonsoft.Json.Linq;
using Prism;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace HOApp.Common.Utils
{
public class CheckForUpdate
{
public static async void CheckVersion()
{
try
{
Version current_build = new Version(VersionTracking.CurrentVersion);
Version store_build = new Version("1.0.0");
if (Device.RuntimePlatform == Device.iOS)
{
var version = await GetITunesAppVersion();
store_build = version ?? new Version("1.0.0");
}
else if (Device.RuntimePlatform == Device.Android)
{
var version = await GetGoogleAppVersion();
store_build = version ?? new Version("1.0.0");
}
if (store_build != new Version("1.0.0") && store_build > current_build)
{
var confirmed = await PrismApplicationBase.Current.MainPage.DisplayAlert(StringResources.AppUpdateHeader, StringResources.AppUpdateContent, StringResources.AppUpdateConfirm , StringResources.Cancel);
if (!confirmed)
{
return;
}
InstallNewVersion();
}
}
catch (Exception ex)
{
}
}
public static async Task<Version> GetITunesAppVersion()
{
try
{
string ITunesStoreURL = $"{AppConfig.ItunesAppStoreUrl}";
Version ITunesVersion = null;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(ITunesStoreURL);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, ITunesStoreURL);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
string result = await response.Content.ReadAsStringAsync();
JObject jobj = JObject.Parse(result);
ITunesVersion = new Version(jobj.SelectToken("results[0].version").Value<string>());
}
}
return ITunesVersion;
}
catch (Exception ex)
{
Console.WriteLine(ex);
return null;
}
}
public static async void InstallNewVersion()
{
if (Device.RuntimePlatform == Device.iOS)
{
await Browser.OpenAsync($"{AppConfig.AppleAppDownloadLink}", BrowserLaunchMode.SystemPreferred);
}
else if (Device.RuntimePlatform == Device.Android)
{
//add google play url once we have it
await Browser.OpenAsync($"{AppConfig.GooglePlayAppDownloadLink}", BrowserLaunchMode.SystemPreferred);
}
}
public static async Task<Version> GetGoogleAppVersion()
{
try
{
Version GoogleVersion = null;
using (var client = new HttpClient())
{
var doc = await client.GetAsync($"{AppConfig.GooglePlayAppStoreUrl}");
if (doc.IsSuccessStatusCode)
{
Regex regex = new Regex(@">[0-9].[0-9].[0-9]<");
var result = await doc.Content.ReadAsStringAsync();
var version_source = result.Substring(result.IndexOf("Current Version"), 200);
Match match = regex.Match(version_source);
if (match.Success)
{
string version = match.Value.Replace("<", "").Replace(">", "");
GoogleVersion = new Version(version);
}
else
{
var version_part1 = version_source.Substring(version_source.IndexOf(".") - 1, 10);
var version_part2 = version_part1.Remove(version_part1.IndexOf("<"));
GoogleVersion = new Version(version_part2);
}
}
}
return GoogleVersion;
}
catch (Exception ex)
{
Console.WriteLine(ex);
return null;
}
}
}
}
///// In your xaml.cs you want to show this popup on simply add
protected override void OnAppearing()
{
base.OnAppearing();
try
{
CheckForUpdate.CheckVersion();
}
catch (Exception ex)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment