iOS自定义版本更新检查

1、效果图

img_cb8e791ae9f67ff8224ebf1b52b7066f.png
提示新版本
img_231e053a7695ee8336266fa91a279f43.png
提示新版本(读取了AppStore写的更新方案)

2、关键点

请求http://itunes.apple.com/lookup?id=968615456,就会返回关于这个应用的详细内容的JSON数据,后面就简单了吧

我的应用返回的JSON数据(已经格式化):

{
    "resultCount": 1,
    "results": [
        {
            "advisories": [
                "Unrestricted Web Access"
            ],
            "artistId": 914453386,
            "artistName": "he tiancong",
            "artistViewUrl": "https://itunes.apple.com/us/artist/he-tiancong/id914453386?uo=4",
            "artworkUrl100": "http://is5.mzstatic.com/image/pf/us/r30/Purple5/v4/6f/89/92/6f8992e4-5c10-be87-9242-d933ddb0780a/mzl.jxbacfmg.png",
            "artworkUrl512": "http://is5.mzstatic.com/image/pf/us/r30/Purple5/v4/6f/89/92/6f8992e4-5c10-be87-9242-d933ddb0780a/mzl.jxbacfmg.png",
            "artworkUrl60": "http://is3.mzstatic.com/image/pf/us/r30/Purple3/v4/2e/cc/a9/2ecca990-b82a-a238-793a-a16b59b56389/AppIcon60x60_U00402x.png",
            "bundleId": "CampusOfGLUT",
            "contentAdvisoryRating": "17+",
            "currency": "USD",
            "description": "桂林理工大学——校园通正式上线!

应用提供:
1、学校新闻、通知等动态;
2、学校学院、学习等、
3、学习 、生活等多栏目,分享和交流平台~

欢迎大家提供建议和评价!

邮件:ihetiancong@qq.com
QQ:3027201361",
            "features": [
                "iosUniversal"
            ],
            "fileSizeBytes": "6436734",
            "formattedPrice": "Free",
            "genreIds": [
                "6017",
                "6012"
            ],
            "genres": [
                "Education",
                "Lifestyle"
            ],
            "ipadScreenshotUrls": [
                "http://a1.mzstatic.com/us/r30/Purple3/v4/cf/36/c1/cf36c120-3798-303d-6ae6-cb70da8c7b18/screen480x480.jpeg",
                "http://a3.mzstatic.com/us/r30/Purple3/v4/4c/34/19/4c3419c4-c777-f202-0f5a-a1e733bf37a8/screen480x480.jpeg",
                "http://a2.mzstatic.com/us/r30/Purple5/v4/9e/9b/35/9e9b3592-4e93-148b-0fd8-73d8ed389388/screen480x480.jpeg"
            ],
            "isGameCenterEnabled": false,
            "kind": "software",
            "languageCodesISO2A": [
                "EN",
                "FR",
                "DE",
                "JA",
                "KO",
                "PL",
                "RU",
                "ZH",
                "ES",
                "ZH",
                "UK"
            ],
            "minimumOsVersion": "8.1",
            "price": 0.0,
            "primaryGenreId": 6017,
            "primaryGenreName": "Education",
            "releaseDate": "2015-03-18T09:34:28Z",
            "releaseNotes": "——————那些年,最后一次更新的版本——————

1、优化iPhone和iPad下显示效果
2、增加话题图标,内容更加醒目
3、增加一些可有可无的更新

——————那些年,我们一起说过的话题——————

1、非常感谢那些年i被拒绝的女孩,她们让我充实成长
2、非常感谢那些年i被关照的老师,他们让我无忧学习
3、非常感谢那些年i被信任的同学,他们让我珍惜想念
4、非常感谢那些年i被坚持的自己,你们让我相信自己

——————最后说明——————

1、应用为i毕业设计作品,不提供商业信息,感谢大家支持
2、应用中的学校新闻只提参考,由于解析原因,可能个别新闻无法显示,真实动态请浏览(www.glut.edu.cn)
3、校园话题的意义是希望能将学校的信息通过移动互联网重新整合在一起,这是一个信息未来的趋势
4、其它问题请联系:ihetiancong@qq.com

——————谢谢大家——————",
            "screenshotUrls": [
                "http://a2.mzstatic.com/us/r30/Purple1/v4/1f/bf/32/1fbf321a-c22b-f938-0d84-17e65730e7a9/screen1136x1136.jpeg",
                "http://a5.mzstatic.com/us/r30/Purple3/v4/c9/92/ec/c992ec71-7576-6334-3ebb-4aa857880219/screen1136x1136.jpeg",
                "http://a1.mzstatic.com/us/r30/Purple5/v4/72/27/ae/7227aed1-62fa-6946-e63e-52090ce6a021/screen1136x1136.jpeg",
                "http://a4.mzstatic.com/us/r30/Purple2/v4/13/46/d3/1346d396-4126-ab48-ae15-b0038b92bd68/screen1136x1136.jpeg"
            ],
            "sellerName": "he tiancong",
            "sellerUrl": "http://weibo.com/iHTCapp",
            "supportedDevices": [
                "iPhone5c",
                "iPhone6Plus",
                "iPodTouchFifthGen",
                "iPadThirdGen4G",
                "iPad2Wifi",
                "iPadFourthGen",
                "iPhone5s",
                "iPhone4S",
                "iPadThirdGen",
                "iPad23G",
                "iPhone6",
                "iPadFourthGen4G",
                "iPhone5",
                "iPadMini",
                "iPadMini4G"
            ],
            "trackCensoredName": "桂林理工大学---校园通",
            "trackContentRating": "17+",
            "trackId": 968615456,
            "trackName": "桂林理工大学---校园通",
            "trackViewUrl": "https://itunes.apple.com/us/app/gui-lin-li-gong-da-xue-xiao/id968615456?mt=8&uo=4",
            "version": "1.3",
            "wrapperType": "software"
        }
    ]
}

3、检查更新Demo

  • 检查更新
[AFNetworkingUtils getRequestWithUrl:@"http://itunes.apple.com/lookup?id=968615456" afterExecute:^(NSDictionary *resultDic) {
 
            float version =[[[[resultDic objectForKey:@"results"] objectAtIndex:0] valueForKey:@"version"] floatValue];
            NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
            float currentVersion = [[infoDic valueForKey:@"CFBundleShortVersionString"] floatValue];

            if(version>currentVersion){
                NSString *alertTitle=[@"桂林理工大学-校园通v" stringByAppendingString:[NSString stringWithFormat:@"%0.1f",version]];
                NSString *alertMsg=@"是否要更新?";
               //NSString *alertMsg  = [[[resultDic objectForKey:@"results"] objectAtIndex:0] valueForKey:@"releaseNotes"]
                UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:alertTitle message:alertMsg delegate:self cancelButtonTitle:@"稍后更新" otherButtonTitles:@"立即更新", nil];
                [alertView show];
            }
    } onError:^{
    }];
  • 跳到AppStore更新
        NSString *url = @"https://itunes.apple.com/us/app/gui-lin-li-gong-da-xue-xiao/id968615456?mt=8&uo=4";
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

4、自定义成一个工具

  • 使用时调用这个方法就行
#pragma mark - 检查版本更新
-(void) checkUpdate{
    
    InformationHandleTool * tool = [InformationHandleTool sharedInfoTool];
    [tool checkUpdateWithAppID:@"968615456" success:^(NSDictionary *resultDic, BOOL isNewVersion, NSString *newVersion) {
        
        if (isNewVersion) {
            [self showUpdateView:newVersion];
        }
        
    } failure:^(NSError *error) {
        ;
    }];

}
  • 网络请求方法-(void)checkUpdateWithAppID:(NSString *)appID success:(void (^)(NSDictionary *resultDic , BOOL isNewVersion , NSString * newVersion))success failure:(void (^)(NSError *error))failure
#pragma mark - 检查版本更新
\- (void)checkUpdateWithAppID:(NSString *)appID success:(void (^)(NSDictionary *resultDic , BOOL isNewVersion , NSString * newVersion))success failure:(void (^)(NSError *error))failure
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    NSString *encodingUrl=[[@"http://itunes.apple.com/lookup?id=" stringByAppendingString:appID] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [manager GET:encodingUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *resultDic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        
         NSString * versionStr =[[[resultDic objectForKey:@"results"] objectAtIndex:0] valueForKey:@"version"];
        
        float version =[versionStr floatValue];
        //self.iTunesLink=[[[resultDic objectForKey:@"results"] objectAtIndex:0] valueForKey:@"trackViewUrl"];
        NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
        float currentVersion = [[infoDic valueForKey:@"CFBundleShortVersionString"] floatValue];
        
        if(version>currentVersion){
            
            success(resultDic, YES, versionStr);
            
        }else{
            
            success(resultDic,NO ,versionStr);
            
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(error);
    }];
}
  • 提醒方法- (void)showUpdateView:(NSString *)newVersion
    ** 注:这个是iOS8以后的UIAlertController **
\- (void)showUpdateView:(NSString *)newVersion
{
    NSString *alertMsg=[[@"桂林理工大学-校园通v" stringByAppendingString:[NSString stringWithFormat:@"%0.1f",[newVersion floatValue]]] stringByAppendingString:@",赶快体验最新版本吧!"];
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"发现新版本" message:alertMsg preferredStyle:UIAlertControllerStyleAlert];
    
    // 添加按钮
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        NSString *str = @"https://itunes.apple.com/us/app/gui-lin-li-gong-da-xue-xiao/id968615456?mt=8&uo=4";
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        
    }]];

    [self presentViewController:alert animated:YES completion:nil];
}

5、检查增强

  • 服务器版本跟本地版本可以随意,比如服务器1.4.1 ,本地1.4,也能检查了。

            NSString * serverVersion =[[[resultDic objectForKey:@"results"] objectAtIndex:0] valueForKey:@"version"];
            NSString * localVersion = [SystemUtils getVersionSting];
            
            //以"."分隔数字然后分配到不同数组
            NSArray * serverArray = [serverVersion componentsSeparatedByString:@"."];
            NSArray * localArray = [localVersion componentsSeparatedByString:@"."];
            
            for (int i = 0; i < serverArray.count; i++) {
                
                //以服务器版本为基准,判断本地版本位数小于服务器版本时,直接返回(并且判断为新版本,比如服务器1.5.1 本地为1.5)
                if(i > (localArray.count -1)){
                   //有新版本,提示!
                     break;
                }
                
                //有新版本,服务器版本对应数字大于本地
                if ( [serverArray[i] intValue] > [localArray[i] intValue]) {
                     //有新版本,提示!
                    break;
                }
            }
  • 如果有什么疑问,可以在评论区一起讨论;
  • 如果有什么不正确的地方,欢迎指导!


注:本文首发于 iHTCboy's blog,如若转载,请注明来源。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值