AppVersion.iOS.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System.Globalization;
  2. using InABox.Core;
  3. using Newtonsoft.Json;
  4. namespace InABox.Avalonia.Platform.iOS
  5. {
  6. public class iOS_AppVersion : IAppVersion
  7. {
  8. string _bundleName => NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleName").ToString();
  9. string _bundleIdentifier => NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleIdentifier").ToString();
  10. string _bundleVersion => NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleShortVersionString").ToString();
  11. private Dictionary<string, object> _appstoreversion = new();
  12. public Logger Logger { get; set; }
  13. public string InstalledVersionNumber() => _bundleVersion;
  14. public async Task<bool> IsUsingLatestVersion()
  15. {
  16. try
  17. {
  18. var info = await GetLatestVersion(false);
  19. return String.IsNullOrWhiteSpace(info.Version)
  20. || (Version.Parse(info.Version).CompareTo(Version.Parse(_bundleVersion)) <= 0);
  21. }
  22. catch (Exception e)
  23. {
  24. Logger?.Error($"Error in IsUsingLatestVersion(): {e.Message}\n{e.StackTrace}");
  25. }
  26. return true;
  27. }
  28. private async Task CheckAppStoreInfo()
  29. {
  30. // http://itunes.apple.com/lookup?bundleId=com.prsdigital.prssiteapp
  31. var url = $"http://itunes.apple.com/lookup?bundleId={_bundleIdentifier}";
  32. using (var request = new HttpRequestMessage(HttpMethod.Get, url))
  33. {
  34. using (var handler = new HttpClientHandler())
  35. {
  36. handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) =>
  37. {
  38. //bypass
  39. return true;
  40. };
  41. using (var client = new HttpClient(handler))
  42. {
  43. using (var responseMsg = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead))
  44. {
  45. if (!responseMsg.IsSuccessStatusCode)
  46. {
  47. throw new LatestVersionException($"Error connecting to the App Store. Url={url}.");
  48. }
  49. try
  50. {
  51. var content = responseMsg.Content == null
  52. ? null
  53. : await responseMsg.Content.ReadAsStringAsync();
  54. var _appstoreinfo =
  55. JsonConvert.DeserializeObject<Dictionary<string, object>>(
  56. content);
  57. if (_appstoreinfo.TryGetValue("results", out var results)
  58. && results is Dictionary<string, object> dict
  59. && dict.FirstOrDefault().Value is Dictionary<string, object> verinfo)
  60. _appstoreversion = verinfo;
  61. }
  62. catch (Exception e)
  63. {
  64. Logger?.Error(
  65. $"Error parsing content from the App Store. Url={url}\n{e.Message}\n{e.StackTrace}");
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }
  72. private AppInfo _info = null;
  73. public async Task<AppInfo> GetLatestVersion(bool force)
  74. {
  75. if (force)
  76. _info = null;
  77. if (_info != null)
  78. return _info;
  79. _info = new AppInfo();
  80. try
  81. {
  82. await CheckAppStoreInfo();
  83. {
  84. if (_appstoreversion.TryGetValue("releaseDate", out var releaseDateObj) &&
  85. releaseDateObj is string releaseDateStr &&
  86. DateTime.TryParse(releaseDateStr, out DateTime releaseDate))
  87. _info.Date = releaseDate;
  88. if (_appstoreversion.TryGetValue("version", out var versionObj) &&
  89. versionObj is string versionStr)
  90. _info.Version = versionStr;
  91. if (_appstoreversion.TryGetValue("releaseNotes", out var releaseNotesObj) &&
  92. releaseNotesObj is string releaseNotesStr)
  93. _info.Notes = releaseNotesStr;
  94. }
  95. }
  96. catch (Exception e)
  97. {
  98. Logger?.Error($"Error in GetLatestVersion(): {e.Message}\n{e.StackTrace}");
  99. }
  100. return _info;
  101. }
  102. /// <inheritdoc />
  103. public Task OpenAppInStore()
  104. {
  105. try
  106. {
  107. var location = RegionInfo.CurrentRegion.Name.ToLower();
  108. if (_appstoreversion.TryGetValue("releaseNotes", out var trackIdObj) &&
  109. trackIdObj is string trackIdStr)
  110. {
  111. var url = String.Format("https://itunes.apple.com/{0}/app/{1}/id{2}?mt=8", location, _bundleName,
  112. trackIdStr);
  113. #if __IOS__
  114. UIKit.UIApplication.SharedApplication.OpenUrl(new NSUrl(url));
  115. #elif __MACOS__
  116. AppKit.NSWorkspace.SharedWorkspace.OpenUrl(new NSUrl($"http://appstore.com/mac/{appName}"));
  117. #endif
  118. }
  119. }
  120. catch (Exception e)
  121. {
  122. Logger?.Error($"Error in OpenAppInStore(): {e.Message}\n{e.StackTrace}");
  123. }
  124. return Task.FromResult(true);
  125. }
  126. }
  127. }