检查版本更新的方法
2015-10-30 16:52
183 查看
中心点:比较线上的版本号的每一位 与现在程序中的每一位是否相同 从后到前不同位置更新的程度不同。提示的语言也不同。中间的参数kStoreAppId 是APPID。
#pragma mark 检查版本更新
+(void)checkAppUpdate
{
//线程
dispatch_queue_t appleQueue =
dispatch_queue_create("apple",
DISPATCH_QUEUE_CONCURRENT);
dispatch_async(appleQueue, ^{
//进入appleQueue的子线程了,在这儿爱干嘛干嘛,记得干完了回主线程
NSDictionary *infoDict = [[NSBundle
mainBundle] infoDictionary];
NSString *nowVersion = [infoDict
objectForKey:@"CFBundleShortVersionString"];
NSURL *url = [NSURL
URLWithString:[NSString
stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",
kStoreAppId]];
NSString * file = [NSString
stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:nil];
if (file==nil||file.length==0)
{
return;
}
NSDictionary *dicFile=[self
dictionaryWithJsonString:file];
if (dicFile==nil) {
return;
}
NSArray *arrResult=[dicFile
objectForKey:@"results"];
if (arrResult==nil||arrResult.count==0)
{
return;
}
NSDictionary *dicResult=[arrResult
objectAtIndex:0];
if (dicResult==nil) {
return;
}
if (![[dicResult
allKeys]containsObject:@"version"]) {
return;
}
///appStore版本
NSString *newVersion =[dicResult
objectForKey:@"version"];
NSInteger lastVersionF = [[[newVersion
componentsSeparatedByString:@"."]
objectAtIndex:0]
integerValue];
NSInteger lastVersionS = [[[newVersion
componentsSeparatedByString:@"."]
objectAtIndex:1]
integerValue];
NSInteger lastVersionT = [[[newVersion
componentsSeparatedByString:@"."]
objectAtIndex:2]
integerValue];
///本地版本
NSInteger nowVersionF=[[[nowVersion
componentsSeparatedByString:@"."]
objectAtIndex:0]
integerValue];
NSInteger nowVersionS=[[[nowVersion
componentsSeparatedByString:@"."]
objectAtIndex:1]
integerValue];
NSInteger nowVersionT=[[[nowVersion
componentsSeparatedByString:@"."]
objectAtIndex:2]
integerValue];
NSString *str_Title=@"";
NSString *newDes=@"";
if (nowVersionF < lastVersionF) {
str_Title=@"软件全面更新提示";
newDes=[NSString
stringWithFormat:@"系统全面升级\n请重新下载新版本应用!\n
更新内容:\n %@",[dicResult
objectForKey:@"releaseNotes"]];
UIAlertView *alert = [[UIAlertView
alloc] initWithTitle:str_Title
message:newDes delegate:self
cancelButtonTitle:@"暂不更新"otherButtonTitles:@"立即更新",nil];
alert.tag=tag_AlertView_CheckVersion;
[alert show];
return;
}
if (nowVersionF <= lastVersionF && nowVersionS < lastVersionS) {
str_Title=@"软件重要更新提示";
newDes=[NSString
stringWithFormat:@"有新的版本更新,是否前往更新?\n更新内容:\n %@",[dicResult
objectForKey:@"releaseNotes"]];
UIAlertView *alert = [[UIAlertView
alloc] initWithTitle:str_Title
message:newDes delegate:self
cancelButtonTitle:@"暂不更新"otherButtonTitles:@"立即更新",nil];
alert.tag=tag_AlertView_CheckVersion;
[alert show];
return;
}
if (nowVersionF <= lastVersionF && nowVersionS <= lastVersionS && nowVersionT < lastVersionT) {
str_Title=@"软件优化更新提示";
newDes=[NSString
stringWithFormat:@"有新的版本更新,是否前往更新?\n
更新内容:\n %@",[dicResult
objectForKey:@"releaseNotes"]];
UIAlertView *alert = [[UIAlertView
alloc] initWithTitle:str_Title
message:newDes delegate:self
cancelButtonTitle:@"暂不更新"otherButtonTitles:@"立即更新",nil];
alert.tag=tag_AlertView_CheckVersion;
[alert show];
return;
}
//完事儿了,回主线程
dispatch_async(dispatch_get_main_queue(), ^{
});
});
}
#pragma mark 检查版本更新
+(void)checkAppUpdate
{
//线程
dispatch_queue_t appleQueue =
dispatch_queue_create("apple",
DISPATCH_QUEUE_CONCURRENT);
dispatch_async(appleQueue, ^{
//进入appleQueue的子线程了,在这儿爱干嘛干嘛,记得干完了回主线程
NSDictionary *infoDict = [[NSBundle
mainBundle] infoDictionary];
NSString *nowVersion = [infoDict
objectForKey:@"CFBundleShortVersionString"];
NSURL *url = [NSURL
URLWithString:[NSString
stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",
kStoreAppId]];
NSString * file = [NSString
stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:nil];
if (file==nil||file.length==0)
{
return;
}
NSDictionary *dicFile=[self
dictionaryWithJsonString:file];
if (dicFile==nil) {
return;
}
NSArray *arrResult=[dicFile
objectForKey:@"results"];
if (arrResult==nil||arrResult.count==0)
{
return;
}
NSDictionary *dicResult=[arrResult
objectAtIndex:0];
if (dicResult==nil) {
return;
}
if (![[dicResult
allKeys]containsObject:@"version"]) {
return;
}
///appStore版本
NSString *newVersion =[dicResult
objectForKey:@"version"];
NSInteger lastVersionF = [[[newVersion
componentsSeparatedByString:@"."]
objectAtIndex:0]
integerValue];
NSInteger lastVersionS = [[[newVersion
componentsSeparatedByString:@"."]
objectAtIndex:1]
integerValue];
NSInteger lastVersionT = [[[newVersion
componentsSeparatedByString:@"."]
objectAtIndex:2]
integerValue];
///本地版本
NSInteger nowVersionF=[[[nowVersion
componentsSeparatedByString:@"."]
objectAtIndex:0]
integerValue];
NSInteger nowVersionS=[[[nowVersion
componentsSeparatedByString:@"."]
objectAtIndex:1]
integerValue];
NSInteger nowVersionT=[[[nowVersion
componentsSeparatedByString:@"."]
objectAtIndex:2]
integerValue];
NSString *str_Title=@"";
NSString *newDes=@"";
if (nowVersionF < lastVersionF) {
str_Title=@"软件全面更新提示";
newDes=[NSString
stringWithFormat:@"系统全面升级\n请重新下载新版本应用!\n
更新内容:\n %@",[dicResult
objectForKey:@"releaseNotes"]];
UIAlertView *alert = [[UIAlertView
alloc] initWithTitle:str_Title
message:newDes delegate:self
cancelButtonTitle:@"暂不更新"otherButtonTitles:@"立即更新",nil];
alert.tag=tag_AlertView_CheckVersion;
[alert show];
return;
}
if (nowVersionF <= lastVersionF && nowVersionS < lastVersionS) {
str_Title=@"软件重要更新提示";
newDes=[NSString
stringWithFormat:@"有新的版本更新,是否前往更新?\n更新内容:\n %@",[dicResult
objectForKey:@"releaseNotes"]];
UIAlertView *alert = [[UIAlertView
alloc] initWithTitle:str_Title
message:newDes delegate:self
cancelButtonTitle:@"暂不更新"otherButtonTitles:@"立即更新",nil];
alert.tag=tag_AlertView_CheckVersion;
[alert show];
return;
}
if (nowVersionF <= lastVersionF && nowVersionS <= lastVersionS && nowVersionT < lastVersionT) {
str_Title=@"软件优化更新提示";
newDes=[NSString
stringWithFormat:@"有新的版本更新,是否前往更新?\n
更新内容:\n %@",[dicResult
objectForKey:@"releaseNotes"]];
UIAlertView *alert = [[UIAlertView
alloc] initWithTitle:str_Title
message:newDes delegate:self
cancelButtonTitle:@"暂不更新"otherButtonTitles:@"立即更新",nil];
alert.tag=tag_AlertView_CheckVersion;
[alert show];
return;
}
//完事儿了,回主线程
dispatch_async(dispatch_get_main_queue(), ^{
});
});
}
相关文章推荐
- Spring Security教程(8)---- 自定义决策管理器及修改权限前缀
- js 如何将某个属性设置成隐藏的
- 重定向和转发
- 安卓模拟器
- [国嵌攻略][042][代码搬移不可少]
- UUID of GATT service
- 加密技术
- VC获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法
- 使用MediaRecorder类实现录音的功能
- js小案例效果学习记录--倒计时
- 机器学习i
- 【Win10 UWP】后台任务与动态磁贴
- move zeros leetcode
- ionic侧边栏 ion-side-menus 以及 ion-tap结合侧边 栏详解
- android打造仿qq控件可拉伸头部控件
- java正则匹配html片段,并取得标签中的内容
- Spring Security教程外篇(1)---- AuthenticationException异常详解
- 关于循环队列的一些讲解
- 【经验】使用Profiler工具分析内存占用情况
- Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法