AppVersion.iOS.cs 5.8 KB

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