美文网首页
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://blog.csdn.net/qq_34963282/article/details/7923...

  • App 版本更新

    App更新流程:检查更新==>提示更新==>下载apk==>安装新版App 检查更新:根据不同的业务和后台逻辑实现...

  • app版本更新

    实战版本更新(okhttp3、service、notification) https://github.com/c...

  • APP版本更新

    原文链接1原文链接2 iOS程序自动提示更新的实现方案大致分为两种: 第一种,自己服务器提供一个接口,告知相关ap...

  • APP版本更新

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

  • iOS版本更新

    iOS APP版本 更新

  • Android更新App版本、更新Android版本

    库地址: https://github.com/YaYaG/LightUpdateApp 集成方式 : 1 引入 ...

  • ionic2实现APP自动更新

    app版本更新可以在应用的设置里面检查版本进行更新或者进入app后自动检查更新。我这里实现的是自动检查更新。 第一...

  • ReactNative App更新下载(Android+iOS)

    APP涉及到版本更新(非热更新),版本检测下载App,Android和iOS实现方式不同 1.Android直接和...

  • APP版本更新的一个解决方案

    一、APP版本迭代更新问题 在处理APP版本迭代时,因为苹果手机上用户是可以手动关闭APP Store自动更新功能...

网友评论

      本文标题:APP版本更新

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