Created
November 29, 2022 19:28
-
-
Save sjehutch/d2f35259d72f6cb4b97f70cfc119472f to your computer and use it in GitHub Desktop.
Looking up App Store version Xamarin Forms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static async Task<Version> GetITunesAppVersion() | |
{ | |
try | |
{ | |
string ITunesStoreURL = @"https://itunes.apple.com"; | |
string route = @"lookup?bundleId=com.cinc.hoapp"; | |
Version ITunesVersion = null; | |
using (var client = new HttpClient()) | |
{ | |
client.BaseAddress = new Uri(ITunesStoreURL); | |
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, route); | |
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 Task<Version> GetGoogleAppVersion() | |
{ | |
try | |
{ | |
Version GoogleVersion = null; | |
using (var client = new HttpClient()) | |
{ | |
var doc = await client.GetAsync("https://play.google.com/store/apps/details?id=com.cinc.hoapp&hl=en_CA"); | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment