之前项目里使用到了版本更新,所以在这里记录下:
//
// ViewController.m
// VersionDemo
//
// Created by zhengll on 16/5/24.
// Copyright © 2016年 TipTimes-1. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UIAlertViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self verSionMethd];
}
-(void) verSionMethd{
NSDictionary * infoDic = [[NSBundle mainBundle] infoDictionary];
NSString * currentVersion = [infoDic objectForKey:@"FBundleVersion"];
NSString * URL = @"http://itunes.apple.com/cn/lookup?id=APPID";//如果是国内的版本需要加上/cn/
//APPID 是在AppStore上面的appid
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:URL]];
[request setHTTPMethod:@"POST"];
NSHTTPURLResponse * urlResponse = nil;
NSError * error = nil;
NSData * recerveData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString * results = [[NSString alloc] initWithBytes:[recerveData bytes] length:[recerveData length] encoding:NSUTF8StringEncoding];
NSData * jsonData = [results dataUsingEncoding:NSUTF8StringEncoding];
// NSError * err;
NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
NSArray * infoArray = [dic objectForKey:@"results"];
if ([infoArray count]) {
NSDictionary * releaseInfo = [infoArray objectAtIndex:0];
NSString * lastVersion = [releaseInfo objectForKey:@"version"];
float last = [lastVersion floatValue];
float current = [currentVersion floatValue];
if (![lastVersion isEqualToString:currentVersion] && (last > current)) {//在此添加一个判断条件,这样就可以保证,当当前版本低于AppStore版本时进行提示,同时也防止审核不通过,因为现在apple审核不建议添加自定义的版本更新提示
NSLog(@"新版本%@,当前版本%@",lastVersion,currentVersion);
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"新版本%@已发布!",lastVersion] delegate:self cancelButtonTitle:@"知道了" otherButtonTitles: nil];
alertView.delegate = self;
[alertView addButtonWithTitle:@"前往更新"];
[alertView show];
alertView.tag = 20;
}else{
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:nil message:@"已是最高版本" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
[alertView show];
}
}
}
-(void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1&& alertView.tag == 20) {
NSString * url = @"url";//url是app 在 AppStore上面的下载地址
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end