美文网首页eiYoiOSMac
iOS自定义版本更新检查

iOS自定义版本更新检查

作者: iHTCboy | 来源:发表于2015-04-26 13:01 被阅读8685次

1、效果图

提示新版本 提示新版本(读取了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,如若转载,请注明来源。

相关文章

  • iOS自定义版本更新检查

    1、效果图 2、关键点 请求http://itunes.apple.com/lookup?id=968615456...

  • iOS 检查App版本更新

    可以通过 iTunes 接口检查 App 版本更新,接口为: 请求返回数很多,其中重要信息如下: 然后通过当前Ap...

  • iOS 版本检查更新(iVersion)

    iVersion的使用 在这里介绍一个版本检查更新三方库的使用,在github上已经1.9K星了,使用也是非常的简...

  • iOS APP检查版本更新

    iOS程序自动提示更新的实现方案大致分为两种:第一种,自己服务器提供一个接口,告知相关app的当前版本,是否需要更...

  • iOS APP检查版本更新

    在开发中,我们可能会遇到这样的需求,当AppStore中有新版本需要更新迭代,用户在点开APP的时候弹出提示框提醒...

  • iOS 检查App版本更新

  • iOS-OC版本更新/检查更新

    自己挖的坑自己要填上...iOS是只能自动提醒更新,界面上不能有按钮提供给用户手动更新的,自己失误在设置里面的版本...

  • 归档archive

    更新swift版本 加载 保存 自定义对象的归档,使用iOS11的API 自定义类Book,遵守NSSecureC...

  • 检查版本更新

    问题:最近因为后端接口调整,没有适配老版本,导致线上bug不断.因为我们之前版本更新用的三方框架,会提醒用户到Ap...

  • 苹果审核被拒原因

    1.应用内包含检查更新内容 iOS 应用的版本更新必须通过 App Store 进行,自身 App 内不能包含提示...

网友评论

  • 2f6633eb6ca7:我app第一次上架 上架成功之后App Store可以下载 但是http://itunes.apple.com/lookup?id=我appid 请求数据为空 是为什么 官方延迟么?
    iHTCboy:@白泽青树 嗯,如果是刚上架,要24小时后才有数据的,这是苹果服务器数据同步时间。
  • 追沐:跳转到Appstore后,还需要用户手动选择“更新”,才会更新是吗?那如果用户这时候没有选择更新,又切回App了,这种有回调方法或者监控什么的吗?
  • 深刻的你:我靠,桂工的?
    iHTCboy:@蒙江明 哈哈,校友!
  • iamgaoshuai: [self presentViewController:alert animated:YES completion:nil]; 在工具类中怎么调用这个方法
    iHTCboy:@codeShuai 只能自己传入self了,或者外部调用显示。
  • 萧旭:楼主大人,有没有碰见过这个问题?

    点击去更新后,会跳到appstore中,但是不显示“更新”,而是显示“打开”;

    您或许会说没提交成功,我可以说提交成功了,因为可以在appstore自己的更新部分中更新;
    iHTCboy:@JeanRookieXu 苹果的更新生效的话,有一定的时间,有一定的缓存的,不是发布就马上生效的,所以等一下就会可以了。
  • 张俊一:不错
  • 巴图鲁:厉害
  • 方不慌_:对啊楼主,写完带个demo呗,咱新手还是想参照demo:smile::smile:
    iHTCboy:@_Fxa 我的开源的项目有,https://github.com/iHTCboy
  • leftwater:现在上线都会被拒了 appstore会提示更新
    Maxxin:@leftwater 那怎么办呢,请问你有解决的办法吗
    leftwater:@NtZheng 是的 包含到更新字眼都会被拒绝掉了
    郑明明:@leftwater 是吗?真的吗,我最近项目需要搞这个呢?
  • 朋友有朋:437913028@qq.com,邮箱打错了
    iHTCboy:@朋友有朋 我的开源的项目有,https://github.com/iHTCboy
  • 朋友有朋:楼主完整的demo可否发一份,437910328@qq.com
  • 咖啡bu加糖:能把完整的代码发给我看看么,想学习下
    iHTCboy:@李大宝是个小胖子 我的开源项目有,https://github.com/iHTCboy
  • 我叫大大虾米:请问如果有种情况,强制用户更新,怎么搞
    iHTCboy:@我叫大大虾米 进入应用请求最新版本,然后可以在新版本里加一些标志,比如有“强烈要求更新”,这样的文字时,强制用户更新。
  • hrscy:上传到app store可以自动更新吧
    iHTCboy:@hrscy 可以的。
  • 寂静的天空:请问楼主 这样不会被拒么 不是说苹果禁止自动提示跟新么?
    Charles___:@寂静的天空 可以提示,不会
  • b8765e130bb0:Demo ?
    iHTCboy:@静阳 我的开源的项目有,https://github.com/iHTCboy
    iHTCboy:@静阳 核心代码写出来了,有时候在写demo咯😁
  • 煜寒了:@liuwin7 企业账号可以挂在自己的服务器做版本比对就好了
  • liuwin7:如果是企业账号发布的应用怎么去检查更新
    iHTCboy:@liuwin7 企业的只能自己服务器来做比较啦

本文标题:iOS自定义版本更新检查

本文链接:https://www.haomeiwen.com/subject/rgkoxttx.html