ios 版本更新迭代 前端根据版本号判断来提示用户更新

网上很多ios版本更新的帖子,但是很多都不用,今天有个人问我这块的问题,我就把我之前写的给他了, 其实道理很简单就是拿2个版本号比大小,比较简单,没啥逻辑。 废话不多说,直接上代码,希望可以帮到大家。 

//检测新版本

-(void)IterationVersion {

NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];

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

NSString *URL = @"https://itunes.apple.com/lookup?id=1106225692";

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

[request setURL:[NSURL URLWithString:URL]];

[request setHTTPMethod:@"POST"];

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

if (data != nil) {

NSMutableDictionary *jsondata = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];

NSArray *infoArray = [jsondata objectForKey:@"results"];

if ([infoArray count]) {

NSDictionary *releaseInfo = [infoArray objectAtIndex:0];

NSString *lastVersion = [releaseInfo objectForKey:@"version"];

NSString *trackViewUrl = [releaseInfo objectForKey:@"trackViewUrl"];

self.trackViewUrl = trackViewUrl;

int  lastVersionnum = [[lastVersion stringByReplacingOccurrencesOfString:@"." withString:@""]intValue];

int  currentVersionnum = [[currentVersion stringByReplacingOccurrencesOfString:@"." withString:@""] intValue];

if (lastVersionnum > currentVersionnum) {

dispatch_sync(dispatch_get_main_queue(), ^(){

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"有新的版本更新,是否前往更新?" delegate:self cancelButtonTitle:@"更新" otherButtonTitles:@"取消", nil];

alert.tag = 10000;

[alert show];

});

}

}

}

}];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

if (alertView.tag==10000) {

if (buttonIndex==0) {

NSURL *url = [NSURL URLWithString:self.trackViewUrl];

[[UIApplication sharedApplication]openURL:url];

}

}

}

使用起来也比较简单,直接在AppDelegate的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
}代理方法里 调用即可[self IterationVersion];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS开发系列--网络开发 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博、微信等,这些应用本身可...
    lichengjin阅读 3,721评论 2 7
  • 技术无极限,从菜鸟开始,从源码开始。 由于公司目前项目还是用OC写的项目,没有升级swift 所以暂时SDWebI...
    充满活力的早晨阅读 12,689评论 0 2
  • iOS支付 iOS支付分为两类,第三方支付和应用内支付(内购)。 第三方支付包括:支付宝支付、微信支付、银联支付、...
    请输入账号名阅读 6,278评论 3 22
  • //需要AFN //.h //AFNetworking + (void)post:(NSString *)url ...
    CHADHEA阅读 797评论 0 0
  • 骑着跛脚的马儿 慢慢悠悠慢慢悠悠的晃荡 走过荒无人烟的川藏 站在距天一尺的珠峰 血泪刺痛了我的神经 扣问上苍,关于...
    周门近安阅读 309评论 0 12