如果要创建自己的VersionChecker,在第一个视图控制器的ViewDidLoad的override中,可以调用CheckVersion()函数
public override void ViewDidLoad()
{
base.ViewDidLoad();
CheckVersion();
OtherCode();
}
然后在 CheckVersion 函数中你可以做这样的事情
public async void CheckVersion()
{
var newVersionAsString = new VersionObject();
string responseContent = string.Empty;
using (HttpClient httpClient = new HttpClient())
{
try
{
var versionCheckUri = "https://gist.githubusercontent.com/saamerm/af01aec0187d38a22fdd9b4378afc9c3/raw/680332a9a45eb015bc17fbf817fbac537348e3d4/Version.json"
using (HttpResponseMessage httpResponse = await httpClient.GetAsync(versionCheckUri))
{
if (httpResponse.StatusCode == System.Net.HttpStatusCode.OK)
{
responseContent = await httpResponse.Content.ReadAsStringAsync();
newVersionAsString = JsonConvert.DeserializeObject<VersionObject>(responseContent);
}
}
}
catch (Exception)
{
}
}
var currentVersion = new Version(NSBundle.MainBundle.InfoDictionary["CFBundleShortVersionString"].ToString());
if (currentVersion != null && !string.IsNullOrWhiteSpace(newVersionAsString) && (new Version(newVersionAsString.latestVersion)) > currentVersion)
{
string AppleStoreAppUrl = "itms-apps://itunes.apple.com/us/app/pages/{YOURAPPURL}?mt=8";
var alert = new AlertView { ViewController = this, Title = "Update Required", Message = "To continue using this mobile app, please install the latest version." };
alert.AddAction("Update Now", ActionStyle.Cancel, (action) => { UIApplication.SharedApplication.OpenUrl(new NSUrl(AppleStoreAppUrl)); this.Dispose(); }).Show();
}
我们从 jsonutils.com 获得的 VersionObject
public class VersionObject
{
public string latestVersion { get; set; }
}
请在使用前测试一下,我可以看到它可能无法正常工作的实例