一句代码更新app
获取线上app版本信息
链接:http://itunes.apple.com/cn/lookup?id=%@ ;id表示app的apppid。
设备上app版本信息
//获取本地软件的版本号
NSString *localVersion = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleShortVersionString"];
跳转appstore更新程序
https://itunes.apple.com/cn/app/appName/id%@?mt=8&ls=1
appName: app的名字
id: app的apppid
直接上代码吧!
+ (void)appUpdateWithAPPID:(NSString *)appid
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
{
NSString *newVersion;
//添加应用的ID
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",appid]];
//通过url获取数据
NSString *jsonResponseString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSData*jsonData = [jsonResponseString dataUsingEncoding:NSUTF8StringEncoding];
//解析json数据为数据字典
NSDictionary *loginAuthenticationResponse;
if (jsonData) {
loginAuthenticationResponse =[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
}else{
return;
}
//从数据字典中检出版本号数据
NSArray *configData = [loginAuthenticationResponse valueForKey:@"results"];
for(id config in configData)
{
newVersion = [config valueForKey:@"version"];
}
//获取本地软件的版本号
NSString *localVersion = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleShortVersionString"];
NSString *msg = [NSString stringWithFormat:@"您当前的版本是V%@,发现新版本V%@,是否下载新版本?",localVersion,newVersion];
//对比发现的新版本和本地的版本
if ([newVersion floatValue] > [localVersion floatValue])
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"升级提示!" message:msg preferredStyle:UIAlertControllerStyleAlert] ;
UIAlertAction *ActionTrue = [UIAlertAction actionWithTitle:@"现在升级" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *strUrl = [NSString stringWithFormat:@"https://itunes.apple.com/cn/app/NSWJBtVerify/id%@?mt=8&ls=1",appid];
#ifdef IOS9_Update
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString: @"?!@#$^&%*+,:;='\"`<>()[]{}/\\| "];
strUrl = [strUrl stringByAddingPercentEncodingWithAllowedCharacters:characterSet];
#else
strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
#endif
#ifdef IOS10_Update
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:strUrl] options:@{UIApplicationOpenURLOptionUniversalLinksOnly : @NO} completionHandler:^(BOOL success) {
if (!success) {
UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"跳转失败情重新尝试!" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定"style:UIAlertActionStyleCancel handler:nil];
[aler addAction:cancelAction];
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:aler animated:YES completion:nil];
});
}
}];
#else
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:strUrl]];
#pragma clang diagnostic pop
#endif
}];
UIAlertAction *ActionFalse = [UIAlertAction actionWithTitle:@"下次" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alertView addAction:ActionFalse];
[alertView addAction:ActionTrue];
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertView animated:YES completion:nil];
});
});
}
else{
}
});
}