首页 > 其他分享 >iOS通过iTunes search检测版本更新,并提示用户更新!

iOS通过iTunes search检测版本更新,并提示用户更新!

时间:2022-12-25 21:04:32浏览次数:63  
标签:search trackViewUrl app iOS results 应用程序 itunes NSString 更新


如果我们要检测app版本的更新,那么我们必须获取当前运行app版本的版本信息和appstore 上发布的最新版本的信息。



当前运行版本信息可以通过info.plist文件中的bundle version中获取:



1. NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];  
2. CFShow(infoDic);
3.
4. "CFBundleVersion"];


这样就获取到当前运行的app的版本了

要获取当前app store上的最新的版本,有两种方法,

一、在某特定的服务器上,发布和存储app最新的版本信息,需要的时候向该服务器请求查询。

二、从app store上查询,可以获取到app的作者,连接,版本等。​​​

​官方相关文档​

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

具体步骤如下:
1,用 POST 方式发送请求:
​​​http://itunes.apple.com/search?term=​​​你的应用程序名称&entity=software

更加精准的做法是根据 app 的 id 来查找:
​​​http://itunes.apple.com/lookup?id=​​你的应用程序的ID

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

你的应用程序的ID 是 itunes connect里的 Apple ID

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

{  
resultCount = 1;
results = (
{
artistId = 开发者 ID;
artistName = 开发者名称;
price = 0;
isGameCenterEnabled = 0;
kind = software;
languageCodesISO2A = (
EN
);
trackCensoredName = 审查名称;
trackContentRating = 评级;
trackId = 应用程序 ID;
trackName = 应用程序名称";
trackViewUrl = 应用程序介绍网址;
userRatingCount = 用户评级;
userRatingCountForCurrentVersion = 1;
version = 版本号;
wrapperType = software;
}
);
}



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

NSDictionary *jsonData = [dataPayload JSONValue];  
NSArray *infoArray = [jsonData objectForKey:@"results"];
NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
NSString *latestVersion = [releaseInfo objectForKey:@"version"];
NSString *trackViewUrl = [releaseInfo objectForKey:@"trackViewUrl"];


如果你拷贝 trackViewUrl 的实际地址,然后在浏览器中打开,就会打开你的应用程序在 appstore 中的介绍页面。当然我们也可以在代码中调用 safari 来打开它。
UIApplication *application = [UIApplication sharedApplication];  
[application openURL:[NSURL URLWithString:trackViewUrl]];  


    1. -(void)onCheckVersion:(NSString *)currentVersion  
    2. {
    3. NSString *URL = APP_URL;
    4. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    5. [request setURL:[NSURL URLWithString:URL]];
    6. "POST"];
    7. NSHTTPURLResponse *urlResponse = nil;
    8. NSError *error = nil;
    9. NSData *recervedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    10.
    11. NSString *results = [[NSString alloc] initWithBytes:[recervedData bytes] length:[recervedData length] encoding:NSUTF8StringEncoding];
    12. NSDictionary *dic = [results JSONValue];
    13. "results"];
    14. if ([infoArray count]) {
    15. NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
    16. "version"];
    17.
    18. if (![lastVersion isEqualToString:currentVersion]) {
    19. "trackVireUrl"];
    20. "更新" message:@"有新的版本更新,是否前往更新?" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"更新", nil] autorelease];
    21. [alert show];
    22. }
    23. }
    24. }
    25.



    当前运行版本信息可以通过info.plist文件中的bundle version中获取:


    1. NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];  
    2. CFShow(infoDic);
    3.
    4. "CFBundleVersion"];


    这样就获取到当前运行的app的版本了

    要获取当前app store上的最新的版本,有两种方法,

    一、在某特定的服务器上,发布和存储app最新的版本信息,需要的时候向该服务器请求查询。

    二、从app store上查询,可以获取到app的作者,连接,版本等。​​官方相关文档​

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

    具体步骤如下:
    1,用 POST 方式发送请求:
    ​​​http://itunes.apple.com/search?term=​​​你的应用程序名称&entity=software

    更加精准的做法是根据 app 的 id 来查找:
    ​​​http://itunes.apple.com/lookup?id=​​你的应用程序的ID

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

    你的应用程序的ID 是 itunes connect里的 Apple ID

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

    {  
    resultCount = 1;
    results = (
    {
    artistId = 开发者 ID;
    artistName = 开发者名称;
    price = 0;
    isGameCenterEnabled = 0;
    kind = software;
    languageCodesISO2A = (
    EN
    );
    trackCensoredName = 审查名称;
    trackContentRating = 评级;
    trackId = 应用程序 ID;
    trackName = 应用程序名称";
    trackViewUrl = 应用程序介绍网址;
    userRatingCount = 用户评级;
    userRatingCountForCurrentVersion = 1;
    version = 版本号;
    wrapperType = software;
    }
    );
    }



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

    NSDictionary *jsonData = [dataPayload JSONValue];  
    NSArray *infoArray = [jsonData objectForKey:@"results"];
    NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
    NSString *latestVersion = [releaseInfo objectForKey:@"version"];
    NSString *trackViewUrl = [releaseInfo objectForKey:@"trackViewUrl"];


    如果你拷贝 trackViewUrl 的实际地址,然后在浏览器中打开,就会打开你的应用程序在 appstore 中的介绍页面。当然我们也可以在代码中调用 safari 来打开它。
    UIApplication *application = [UIApplication sharedApplication];  
    [application openURL:[NSURL URLWithString:trackViewUrl]];  

    1. -(void)onCheckVersion:(NSString *)currentVersion  
    2. {
    3. NSString *URL = APP_URL;
    4. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    5. [request setURL:[NSURL URLWithString:URL]];
    6. "POST"];
    7. NSHTTPURLResponse *urlResponse = nil;
    8. NSError *error = nil;
    9. NSData *recervedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    10.
    11. NSString *results = [[NSString alloc] initWithBytes:[recervedData bytes] length:[recervedData length] encoding:NSUTF8StringEncoding];
    12. NSDictionary *dic = [results JSONValue];
    13. "results"];
    14. if ([infoArray count]) {
    15. NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
    16. "version"];
    17.
    18. if (![lastVersion isEqualToString:currentVersion]) {
    19. "trackVireUrl"];
    20. "更新" message:@"有新的版本更新,是否前往更新?" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"更新", nil] autorelease];
    21. [alert show];
    22. }
    23. }
    24. }

    标签:search,trackViewUrl,app,iOS,results,应用程序,itunes,NSString,更新
    From: https://blog.51cto.com/u_15907570/5968335

    相关文章

    • IOS面试题汇总
      1.Differencebetweenshallowcopyanddeepcopy?
浅复制和深复制的区别?
答案:浅层复制:只复制指向对象的指针,而不复制引用对象本身。
深层复制:复制引用对象本身。
意......
    • Axios
      参考官方文档:Axios中文文档Axios和Fetch 类似,都是基于Promise,用来发送网络求请求的不同的是,Axios是基于Ajax封装的,而Fetch是ES6中的原生API(这也是Fetch目前......
    • Elasticsearch全文检索引擎复习笔记
      Elasticsearch全文检索引擎复习笔记Elasticsearch是一个基于Lucene的搜索引擎。它提供了一个分布式、多租户的全文搜索引擎,能够为应用程序提供实时的、结构化和非结构......
    • GPS数据类型(ROS) Label: Research
      转载自ROS传感器之GPS简介一.传感器分类在自动驾驶或者机器人领域,传感器的使用按照测量对象划分,可分为两大类,一类是测量自身状态,另一类测量环境状态。前者主要包含GPS,IM......
    • elasticsearch 安装8.5.3
      下载地址:https://www.elastic.co/cn/downloads/支持:https://www.elastic.co/cn/support/matrix#elastrcsearch.yml需要注意xpack安全配置修改为:falseingest.geoip.d......
    • 【Redis场景2】缓存更新策略(双写一致)
      在业务初始阶段,流量很少的情况下,通过直接操作数据是可行的操作,但是随着业务量的增长,用户的访问量也随之增加,在该阶段自然需要使用一些手段(缓存)来减轻数据库的压力;所谓遇......
    • 用Android Studio更新Android SDK / Android NDK版本
      以前做安卓开发的时候是用Ecliplse+Androidsdk,每次sdk版本更新的时候都是要去网路上搜索并下载对应的sdk版本(有时候某些站点还不能直接访问,只能另外寻找国内的镜像站点);And......
    • Microsoft 365 开发:2种通用方法禁用SPO Search Bar
      Blog链接:​​​https://blog.51cto.com/13969817​​我们都知道SharePointSearchBar位于每个页面顶部,Microsoft允许网站集管理员使用PowerShell隐藏它,本文将给大家介绍一......
    • Windows环境下最新OpenCV和Contribute代码的联合编译【20190505更新红字】
      目的在于获得并使用最新的完全版本的代码,主要方法是对CMake能够熟练使用,并且对编译等基础支持有所了解。因为这篇博客经过多次修改,所以里面的内容和配图可能有不是完......
    • ElasticSearch系列---【Es的快速入门文档】
      Es的快速入门文档1.对比数据库理解ElasticSearch是面向文档型数据库,一条数据在这里就是一个文档。 注意:从ElasticSearch6.X开始,一个Index下只能包含一个Type,因此,在ElasticS......