static NSString *appid = @"1160216987"; //苹果上架的app id
static NSString *appurl = @"http://itunes.apple.com/lookup?id=%@";
//网络请求
-(void)updatasApp
{
NSString *appurlStr = [NSString stringWithFormat:appurl,appid];
NSURL *url = [NSURL URLWithString:appurlStr];
NSURLRequest *requet = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
__weak AppDelegate *blockself = self;
[NSURLConnection sendAsynchronousRequest:requet queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError)
{
if (connectionError == nil)
{
//这里应该刷新UI了
//1.给数据源数组赋值
//2,赋值结束之后.刷新UI([self.tableView reloadData])
NSDictionary *appInfoDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSArray *resultArray = [appInfoDict objectForKey:@"results"];
if (resultArray.count )
{
NSDictionary *infoDict = [resultArray objectAtIndex:0];
NSString *updateVersion = infoDict[@"version"];
dispatch_async(dispatch_get_main_queue(), ^{
blockself.UpdatesString = infoDict[@"trackViewUrl"];
[blockself getVersion:updateVersion];
});
}
}
}];
}
-(void)getVersion:(NSString *)version
{
NSDictionary *infodic = [[NSBundle mainBundle] infoDictionary];
NSString *CurrentVersion = [infodic objectForKey:@"CFBundleShortVersionString"];
//获取本地的版本号
if ([CurrentVersion isEqualToString:version])
{
}
else{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"新版本更新" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"升级", nil];
alertView.tag = 999;
[alertView show];
}
}
//判断用户点击了哪一个按钮
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex
{
if (alertView.tag == 999) {
if (buttonIndex == 1) { //点击”升级“按钮,就从打开app store上应用的详情页面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.UpdatesString]];
}
}
}