美文网首页
APP版本更新

APP版本更新

作者: BryantWang | 来源:发表于2017-02-03 11:08 被阅读88次

    因为项目中间时间允许,抽时间做了一下app新版本迭代.下面贴一下代码供大家参考,要是感觉还可以请给我一颗鼓励的小星星😝

    //NewVersionHelper.h

    //App版本检测更新

    //Created by BryantWang on 2017/1/24.

    //Copyright © 2017年BryantWang. All rights reserved.

    #import

    typedefvoid(^UpdateAppBlock)(NSDictionary*info);

    @interfaceNewVersionHelper :NSObject

    //检查版本方法

    +(void)CheckAppVersion:(UpdateAppBlock)update;

    @end

    //NewVersionHelper.m

    //App版本检测更新

    //

    //Created by BryantWang on 2017/1/24.

    //Copyright © 2017年BryantWang. All rights reserved.

    //

    #import"NewVersionHelper.h"

    //上线APP的APPID

    #define STOREAPPID @"************"

    @implementationNewVersionHelper

    +(void)CheckAppVersion:(UpdateAppBlock)update

    {

    //先获取当前版本的版本号

    NSDictionary*infoDic = [[NSBundlemainBundle]infoDictionary];

    NSString*currentVersion = infoDic[@"CFBundleShortVersionString"];

    //从网络获取AppStore版本号

    NSError*error;

    NSData*response = [NSURLConnectionsendSynchronousRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:[NSStringstringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",STOREAPPID]]]returningResponse:nilerror:nil];

    //结果为空

    if(response ==nil) {

    NSLog(@"没有网络");

    }

    NSDictionary*appInfoDic = [NSJSONSerializationJSONObjectWithData:responseoptions:NSJSONReadingMutableLeaveserror:&error];

    //查询错误

    if(error) {

    NSLog(@"hsUpdateAppError:%@",error);

    return;

    }

    //获取结果

    NSArray*array = appInfoDic[@"results"];

    if(array.count<1) {

    NSLog(@"此App未上架或者未查询到");

    return;

    }

    NSDictionary*dic = array[0];

    NSString*appStoreVersion = dic[@"version"];

    //打印当前版本号

    NSLog(@"当前版本号:%@\n商店版本号:%@",currentVersion,appStoreVersion);

    //设置版本号

    currentVersion= [currentVersionstringByReplacingOccurrencesOfString:@"."withString:@""];

    if(currentVersion.length==2)

    {

    currentVersion = [currentVersionstringByAppendingString:@"0"];

    }

    elseif(currentVersion.length==1)

    {

    currentVersion = [currentVersionstringByAppendingString:@"00"];

    }

    appStoreVersion = [appStoreVersionstringByReplacingOccurrencesOfString:@"."withString:@""];

    if(appStoreVersion.length==2)

    {

    appStoreVersion = [appStoreVersionstringByAppendingString:@"0"];

    }

    elseif(appStoreVersion.length==1)

    {

    appStoreVersion = [appStoreVersionstringByAppendingString:@"00"];

    }

    //若当前版本小于商店版本则更新

    if([currentVersionfloatValue] < [appStoreVersionfloatValue]) {

    update(dic);

    }else{

    NSLog(@"现在已经是最新的了,还想怎么着");

    }

    }

    //ViewController.m

    //App版本检测更新

    //

    //Created by BryantWang on 2017/1/24.

    //Copyright © 2017年BryantWang. All rights reserved.

    //

    #import"ViewController.h"

    #import"NewVersionHelper.h"

    @interfaceViewController()

    @end

    @implementationViewController

    - (void)viewDidLoad {

    [superviewDidLoad];

    [NewVersionHelperCheckAppVersion:^(NSDictionary*info) {

    NSLog(@"检查到有更新,更新信息是%@",info);

    }];

    }

    - (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    }

    具体代码请登录github下载源码,源码传送门:https://github.com/BryantWang81/APPCheckVersion

    相关文章

      网友评论

          本文标题:APP版本更新

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