static NSString *appid = @"1159247524";
static NSString *appurl =@"http://itunes.apple.com/lookup?id=%@";
@interface AppDelegate ()<UIAlertViewDelegate>
@property(nonatomic,copy)NSString *UpdatesString;
@end
-(void)updatasApp
{
NSString *appurlStr = [NSStringstringWithFormat:appurl,appid];
NSURL *url = [NSURLURLWithString:appurlStr];
NSURLRequest *requet = [[NSURLRequestalloc]initWithURL:urlcachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheDatatimeoutInterval:10];
__weakAppDelegate *blockself = self;
[NSURLConnectionsendAsynchronousRequest:requet queue:[NSOperationQueuemainQueue] completionHandler:^(NSURLResponse *_Nullable response, NSData *_Nullable data, NSError *_Nullable connectionError)
{
if (connectionError ==nil)
{
//这里应该刷新UI了
//1.给数据源数组赋值
//2,赋值结束之后.刷新UI([self.tableView reloadData])
NSDictionary *appInfoDict = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingAllowFragmentserror:nil];
NSArray *resultArray = [appInfoDictobjectForKey:@"results"];
if (resultArray.count )
{
NSDictionary *infoDict = [resultArrayobjectAtIndex:0];
NSString *updateVersion = infoDict[@"version"]; //获取网络的版本号
dispatch_async(dispatch_get_main_queue(), ^{
blockself.UpdatesString = infoDict[@"trackViewUrl"]; //下载链接
[blockself getVersion:updateVersion];
});
}
}
}];
}
-(void)getVersion:(NSString *)version
{
NSDictionary *infodic = [[NSBundlemainBundle] infoDictionary];
NSString *CurrentVersion = [infodicobjectForKey:@"CFBundleShortVersionString"];//获取当前版本
if ([CurrentVersionisEqualToString:version])
{
}
else{
UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:@"新版本更新"message:nildelegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"升级",nil];
alertView.tag = 666;
[alertView show];
}
}
//判断用户点击了哪一个按钮
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex
{
if (alertView.tag == 666) {
if (buttonIndex ==1) { //点击”升级“按钮,就从打开app store上应用的详情页面
[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:self.UpdatesString]];
}
}
}