现在的位置: 首页 > 综合 > 正文

如何向 appstore 查询已发布 APP 的信息?

2017年12月28日 ⁄ 综合 ⁄ 共 3984字 ⁄ 字号 评论关闭

如果我们需要实现版本的 app 自动更新,那么我们需要获取当前运行程序的版本信息和 appstore 里发布的最新版本信息。

当前运行程序的版本信息,可以在 mainBundle 里面获取:

  1. NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];  
  2.     NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"];  


而 appstore 里发布的最新版本信息获取稍微复杂一些,有两种方案,思路都是一样的:

其一:在某个服务器上存储最新发布的版本信息,需要的时候向该服务器查询;

其二:在需要的时候向 appstore 查询;

在这里我来介绍第二种方法:向 appstore 查询应用程序信息,包括作者,版本,app 介绍页面地址等信息。

英文好的同学可以参考 apple 的文档:www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.html

具体步骤如下:

1,用 POST 方式发送请求:

http://itunes.apple.com/search?term=你的应用程序名称&entity=software

更加精准的做法是根据 app 的 id 来查找:

http://itunes.apple.com/lookup?id=你的应用程序的ID


2,从获得的 response 数据中解析需要的数据。因为从 appstore 查询得到的信息是 JSON 格式的,所以需要经过解析。解析之后得到的原始数据就是如下这个样子的:

[html] view
plain
copy

  1. {  
  2.     resultCount = 1;  
  3.     results =     (  
  4.                 {  
  5.             artistId = 301724683;  
  6.             artistName = Citibank;  
  7.             artistViewUrl = "http://itunes.apple.com/us/artist/citibank/id301724683?uo=4";  
  8.             artworkUrl100 = "http://a5.mzstatic.com/us/r1000/117/Purple/a1/85/a9/mzl.hvwnfdkw.png";  
  9.             artworkUrl512 = "http://a5.mzstatic.com/us/r1000/117/Purple/a1/85/a9/mzl.hvwnfdkw.png";  
  10.             artworkUrl60 = "http://a2.mzstatic.com/us/r1000/099/Purple/67/86/7e/mzi.utfdvrgy.png";  
  11.             averageUserRating = "3.5";  
  12.             averageUserRatingForCurrentVersion = 5;  
  13.             contentAdvisoryRating = "4+";  
  14.             currency = USD;  
  15.             description = "Description of you app.";  
  16.             features =             (  
  17.                 iosUniversal  
  18.             );  
  19.             fileSizeBytes = 4141195;  
  20.             genreIds =             (  
  21.                 6015  
  22.             );  
  23.             genres =             (  
  24.                 Finance  
  25.             );  
  26.             ipadScreenshotUrls =             (  
  27.                 "http://a1.mzstatic.com/us/r1000/095/Purple/e0/a6/17/mzl.pbbxcjzt.1024x1024-65.jpg",  
  28.                 "http://a3.mzstatic.com/us/r1000/036/Purple/cc/14/98/mzl.dyairego.1024x1024-65.jpg"  
  29.             );  
  30.             isGameCenterEnabled = 0;  
  31.             kind = software;  
  32.             languageCodesISO2A =             (  
  33.                 EN  
  34.             );  
  35.             price = 0;  
  36.             primaryGenreId = 6015;  
  37.             primaryGenreName = Finance;  
  38.             releaseDate = "2011-01-24T06:14:35Z";  
  39.             releaseNotes = "* View Real-time streaming prices for U.S. Treasuries \n\n* Open and Save your Citi Research in your favorite PDF Reader and Library such as iBooks\n\n* Search for your favorite videos";  
  40.             screenshotUrls =             (  
  41.                 "http://a3.mzstatic.com/us/r1000/066/Purple/17/51/fb/mzl.zywiavgn.png",  
  42.                 "http://a5.mzstatic.com/us/r1000/026/Purple/73/85/97/mzl.csmdtndk.png"  
  43.             );  
  44.             sellerName = "Citibank, N.A.";  
  45.             sellerUrl = "http://";  
  46.             supportedDevices =             (  
  47.                 all  
  48.             );  
  49.             trackCensoredName = "Citi Velocity";  
  50.             trackContentRating = "4+";  
  51.             trackId = 414697122;  
  52.             trackName = "Citi Velocity";  
  53.             trackViewUrl = "http://itunes.apple.com/us/app/citi-velocity/id414697122?mt=8&uo=4";  
  54.             userRatingCount = 5;  
  55.             userRatingCountForCurrentVersion = 1;  
  56.             version = "1.4";  
  57.             wrapperType = software;  
  58.         }  
  59.     );  
  60. }  

然后从中取得 results 数组即可,具体代码如下所示:

  1. NSDictionary *jsonData = [dataPayload JSONValue];  
  2. NSArray *infoArray = [jsonData objectForKey:@"results"];  
  3. NSDictionary *releaseInfo = [infoArray objectAtIndex:0];  
  4. NSString *latestVersion = [releaseInfo objectForKey:@"version"];  
  5. NSString *trackViewUrl = [releaseInfo objectForKey:@"trackViewUrl"];  

如果你拷贝 trackViewUrl 的实际地址然后在浏览器中打开,就会打开你的应用程序在 appstore 中的介绍页面。当然我们也可以在代码中调用 safari 来打开它。

  1. UIApplication *application = [UIApplication sharedApplication];  
  2. [application openURL:[NSURL URLWithString:trackViewUrl]];  

抱歉!评论已关闭.