Version_iOS.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Json;
  3. using System.Net.Http;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net;
  7. using System.IO;
  8. using Foundation;
  9. using UIKit;
  10. using Xamarin.Forms;
  11. using System.Threading.Tasks;
  12. using System.Text.RegularExpressions;
  13. using System.Globalization;
  14. [assembly: Xamarin.Forms.Dependency(typeof(InABox.Mobile.iOS.Version_iOS))]
  15. namespace InABox.Mobile.iOS
  16. {
  17. internal static class iOS_VersionExtensions
  18. {
  19. public static string MakeSafeForAppStoreShortLinkUrl(this string value)
  20. {
  21. // Reference: https://developer.apple.com/library/content/qa/qa1633/_index.html
  22. var regex = new Regex(@"[©™®!¡""#$%'()*+,\\\-.\/:;<=>¿?@[\]^_`{|}~]*");
  23. var safeValue = regex.Replace(value, "")
  24. .Replace(" ", "")
  25. .Replace("&", "and")
  26. .ToLower();
  27. return safeValue;
  28. }
  29. }
  30. /// <summary>
  31. /// <see cref="ILatestVersion"/> implementation for iOS.
  32. /// </summary>
  33. public class Version_iOS : IAppVersion
  34. {
  35. string _bundleName => NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleName").ToString();
  36. string _bundleIdentifier => NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleIdentifier").ToString();
  37. string _bundleVersion => NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleShortVersionString").ToString();
  38. JsonValue _appstoreInfo = null;
  39. /// <inheritdoc />
  40. public string InstalledVersionNumber
  41. {
  42. get => _bundleVersion;
  43. }
  44. private async Task CheckAppStoreInfo(string appName)
  45. {
  46. if (_appstoreInfo != null)
  47. return;
  48. var url = $"http://itunes.apple.com/lookup?bundleId={appName}";
  49. using (var request = new HttpRequestMessage(HttpMethod.Get, url))
  50. {
  51. using (var handler = new HttpClientHandler())
  52. {
  53. handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) =>
  54. {
  55. //bypass
  56. return true;
  57. };
  58. using (var client = new HttpClient(handler))
  59. {
  60. using (var responseMsg = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead))
  61. {
  62. if (!responseMsg.IsSuccessStatusCode)
  63. {
  64. throw new LatestVersionException($"Error connecting to the App Store. Url={url}.");
  65. }
  66. try
  67. {
  68. var content = responseMsg.Content == null ? null : await responseMsg.Content.ReadAsStringAsync();
  69. _appstoreInfo = JsonValue.Parse(content);
  70. }
  71. catch (Exception e)
  72. {
  73. throw new LatestVersionException($"Error parsing content from the App Store. Url={url}.", e);
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }
  80. /// <inheritdoc />
  81. public async Task<bool> IsUsingLatestVersion()
  82. {
  83. string latestversion = string.Empty;
  84. try
  85. {
  86. latestversion = await GetLatestVersionNumber();
  87. return Version.Parse(latestversion).CompareTo(Version.Parse(_bundleVersion)) <= 0;
  88. }
  89. catch (Exception e)
  90. {
  91. throw new LatestVersionException($"Error comparing current app version number with latest. Bundle version={_bundleVersion} and lastest version={latestversion} .", e);
  92. }
  93. }
  94. /// <inheritdoc />
  95. public async Task<string> GetLatestVersionNumber()
  96. {
  97. return await GetLatestVersionNumber(_bundleIdentifier);
  98. }
  99. /// <inheritdoc />
  100. public async Task<string> GetLatestVersionNumber(string appName)
  101. {
  102. if (string.IsNullOrWhiteSpace(appName))
  103. {
  104. throw new ArgumentNullException(nameof(appName));
  105. }
  106. await CheckAppStoreInfo(appName);
  107. var version = _appstoreInfo["results"][0]["version"];
  108. return version;
  109. }
  110. /// <inheritdoc />
  111. public Task OpenAppInStore()
  112. {
  113. return OpenAppInStore(_bundleName);
  114. }
  115. /// <inheritdoc />
  116. public Task OpenAppInStore(string appName)
  117. {
  118. if (string.IsNullOrWhiteSpace(appName))
  119. {
  120. throw new ArgumentNullException(nameof(appName));
  121. }
  122. try
  123. {
  124. var location = RegionInfo.CurrentRegion.Name.ToLower();
  125. var trackid = _appstoreInfo["results"][0]["trackId"];
  126. var url = String.Format("https://itunes.apple.com/{0}/app/{1}/id{2}?mt=8",location,appName,trackid);
  127. //appName = appName.MakeSafeForAppStoreShortLinkUrl();
  128. #if __IOS__
  129. UIKit.UIApplication.SharedApplication.OpenUrl(new NSUrl(url)); // $"http://appstore.com/{appName}"));
  130. #elif __MACOS__
  131. AppKit.NSWorkspace.SharedWorkspace.OpenUrl(new NSUrl($"http://appstore.com/mac/{appName}"));
  132. #endif
  133. return Task.FromResult(true);
  134. }
  135. catch (Exception e)
  136. {
  137. throw new LatestVersionException($"Unable to open {appName} in App Store.", e);
  138. }
  139. }
  140. }
  141. }