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

2017-02-22  本文已影响0人  Alive_5287

网上很多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];

上一篇下一篇

猜你喜欢

热点阅读