天天看点

Xamarin.iOS使用itunes信息更新版本更新

    之前未了解苹果文档中提供了一份itunes网站的http接口api,利用这些api可以轻易的访问到itunes上app应用的信息,对推荐应用,版本信息描述等有所帮助,而不用通过自身平台的方式来提供接口。

   做过的项目中,由于平台没有区分安卓与iOS更新机制的差异,而忽略了iOS应用的更新提醒功能,因此想到借助itunes开放api来获取信息。

1.官方相关文档

www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.htm

2.使用

1)api链接:http://itunes.apple.com/lookup?id=【你的应用程序的ID】

Xamarin.iOS使用itunes信息更新版本更新

2)根据应用ID来获取出来的是精确的信息,应用【yelp】的信息

http://itunes.apple.com/lookup?id=284910350 

看看,json数据结构 ,使用System.Runtime.Serialization.Json解析json数据

Xamarin.iOS使用itunes信息更新版本更新

根据需要添加数据模型,以下代码:

<span style="font-family:SimSun;">using System.IO;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.Text;
using System.Collections.Generic;</span>
           
//itunes信息数据结构
	[DataContract]
	public class AppResult
	{
		[DataMember (Name = "resultCount")]
		public string ResultCount{ get; set; }

		[DataMember (Name = "results")]
		public List<AppInfo> Info{ get; set; }
	}

	[DataContract]
	public class AppInfo
	{
		[DataMember (Name = "releaseNotes")]
		public string Content{ get; set; }

		[DataMember (Name = "version")]
		public string Version{ get; set; }

		[DataMember (Name = "trackName")]
		public string Name{ get; set; }
	}
           

3)字符版本比较

清楚应该获取哪个版本号来做版本对比,itunes上使用的是公开版本号,对应key为【CFBundleShortVersionString】,想了解两个版本

的区别,可看版本号管理

Xamarin.iOS使用itunes信息更新版本更新

简单的比对代码:

public static bool HasNewVersion(string oldVersion,string newVersion)
		{
			try {
				if (!string.IsNullOrEmpty (newVersion) && newVersion.Length == 5) {
					float oldFloat = 0f,newFloat = 0f;
					float.TryParse(oldVersion.Replace(".",""),out oldFloat);
					float.TryParse(newVersion.Replace(".",""),out newFloat);
					if(newFloat > oldFloat){
						return true;
					}
					return false;
				} else {
					return false;
				}
			} catch (Exception ex) {
				Log.Error (ex.Message);
				return false;
			}
		}
           

4)访问Demo

partial void UpdataButton_TouchUpInside (UIButton sender)
		{
			System.Threading.ThreadPool.QueueUserWorkItem ((s) => {
				//当前app版本【8.0.0】
				var oldVerison = NSBundle.MainBundle.InfoDictionary.ObjectForKey (
					new NSString ("CFBundleShortVersionString")).ToString ();

				//获取itunes上信息
				AppResult result = null; 
				var url = "http://itunes.apple.com/lookup?id=284910350";
				var data = NSData.FromUrl (NSUrl.FromString (url));
				Console.WriteLine ("data" + data.ToString ());
				var serializer = new DataContractJsonSerializer (typeof(AppResult));
				using (var stream = new MemoryStream (Encoding.UTF8.GetBytes (data.ToString ()))) {
					result = (AppResult)serializer.ReadObject (stream);
				}
				this.InvokeOnMainThread (() => {
					try {
						if (result != null && result.Info != null && result.Info.Count > 0) {
							var model = result.Info [0];
							if(this.HasNewVersion(oldVerison,model.Version)){
								UIAlertView alertView = new UIAlertView (string.Format ("发现新版本(【Yelp】{0})", model.Version),
									model.Content, null, "关闭", "更新");
								alertView.Show ();
								alertView.Clicked += (obj, e) => {
									if (e.ButtonIndex == 1) {
										//应用地址
									}
								};
							}
						}
					} catch (Exception ex) {
						Console.WriteLine ("Error>>" + ex.Message);
					}

				});
			});
		}
           

效果图

Xamarin.iOS使用itunes信息更新版本更新

继续阅读