Version_iOS.cs 5.1 KB

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