【iOS 开发】利用 iTunes 接口检查 App 版本更新

iOS 想要检查 App 当前版本是否为最新,一般的方案大概都是服务器自己提供一个接口来获取 App 最新版本是多少,然后再做出相应提示是否需要更新,但是接口需要手动维护,应用要审核,还得等审核通过以后才能更新版本号,其实苹果提供了一个 iTunes 接口,能够查到 App 在 AppStore 上的状态信息,既省事又准确,下面记录一下具体实现方法。

接口信息

  • 这是 iTunes 接口地址 ,有兴趣可以看一下,我们要用到的接口如下,xxx 处换成自己 App 的 AppId ,AppId 可以在 iTunes Connect 里面看到。

1
http: //itunes.apple.com/lookup?id=xxx
  • 接口返回的内容有很多,我就挑一些有用的截出来了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
     "resultCount"  1 ,
     "results"  : [{
         "artistId"  "开发者 ID" ,
         "artistName"  "开发者名称" ,
         "trackCensoredName"  "审查名称" ,
         "trackContentRating"  "评级" ,
         "trackId"  "应用程序 ID" ,
         "trackName"  "应用程序名称" ,
         "trackViewUrl"  "应用程序下载网址" ,
         "userRatingCount"  "用户评论数量" ,
         "userRatingCountForCurrentVersion"  "当前版本的用户评论数量" ,
         "version"  "版本号"
     }]
}

实现方法

下面是检查版本更新的具体实现方法,注意接口地址 xxx 处换成自己 App 的 AppId ,App 审核的时候版本肯定是比 AppStore 上高的,所以不用担心审核时会跳出更新提示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/// 检查版本更新
- ( void )checkVersion {
     NSString *url = @ "http://itunes.apple.com/lookup?id=xxx" ;
     [[AFHTTPSessionManager manager] POST:url parameters:nil progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
         DLog(@ "版本更新检查成功" );
         NSArray *results = responseObject[@ "results" ];
         if  (results && results.count >  0 ) {
             NSDictionary *response = results.firstObject;
             NSString *currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@ "CFBundleShortVersionString" ];  // 软件的当前版本
             NSString *lastestVersion = response[@ "version" ];  // AppStore 上软件的最新版本
             if  (currentVersion && lastestVersion && ![self isLastestVersion:currentVersion compare:lastestVersion]) {
                 // 给出提示是否前往 AppStore 更新
                 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@ "提示"  message:@ "检测到有版本更新,是否前往 AppStore 更新版本。"  preferredStyle:UIAlertControllerStyleAlert];
                 [alert addAction:[UIAlertAction actionWithTitle:@ "前往"  style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                     NSString *trackViewUrl = response[@ "trackViewUrl" ];  // AppStore 上软件的地址
                     if  (trackViewUrl) {
                         NSURL *appStoreURL = [NSURL URLWithString:trackViewUrl];
                         if  ([[UIApplication sharedApplication] canOpenURL:appStoreURL]) {
                             [[UIApplication sharedApplication] openURL:appStoreURL];
                         }
                     }
                 }]];
                 [alert addAction:[UIAlertAction actionWithTitle:@ "取消"  style:UIAlertActionStyleCancel handler:nil]];
                 [self.window.rootViewController presentViewController:alert animated:YES completion:nil];
             }
         }
     } failure:^(NSURLSessionDataTask *task, NSError *error) {
         DLog(@ "版本更新检查失败" );
     }];
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/// 判断是否最新版本号(大于或等于为最新)
- (BOOL)isLastestVersion:(NSString *)currentVersion compare:(NSString *)lastestVersion {
     if  (currentVersion && lastestVersion) {
         // 拆分成数组
         NSMutableArray *currentItems = [[currentVersion componentsSeparatedByString:@ "." ] mutableCopy];
         NSMutableArray *lastestItems = [[lastestVersion componentsSeparatedByString:@ "." ] mutableCopy];
         // 如果数量不一样补0
         NSInteger currentCount = currentItems.count;
         NSInteger lastestCount = lastestItems.count;
         if  (currentCount != lastestCount) {
             NSInteger count = labs(currentCount - lastestCount);  // 取绝对值
             for  ( int  i =  0 ; i < count; ++i) {
                 if  (currentCount > lastestCount) {
                     [lastestItems addObject:@ "0" ];
                 else  {
                     [currentItems addObject:@ "0" ];
                 }
             }
         }
         // 依次比较
         BOOL isLastest = YES;
         for  ( int  i =  0 ; i < currentItems.count; ++i) {
             NSString *currentItem = currentItems[i];
             NSString *lastestItem = lastestItems[i];
             if  (currentItem.integerValue != lastestItem.integerValue) {
                 isLastest = currentItem.integerValue > lastestItem.integerValue;
                 break ;
             }
         }
         return  isLastest;
     }
     return  NO;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值