iOS子线程操作检测版本更新,防止阻塞住线程
来源:互联网 发布:linux cat 全部 编辑:程序博客网 时间:2024/06/05 03:16
在做 app 版本更新时发现 请求特别慢,有时甚至等10多秒,严重阻塞主线程,这严重影响了用户体验;
下面介绍一种方法在子线程执行app 版本更新代码;
NSString const *iTnuesApi = @"http://itunes.apple.com/lookup"; //itunes 的链接
//获取本地的版本号
{
NSString *localVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
return localVersion;
}
//执行更新
+ (void )check_APP_UPDATE_WITH_APPID:(NSString *)appid //传入 app 在appstore 的 id
{
__block id JSON = nil;
//创建全局线程
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *dataError = nil;
NSString *appURLAPI = [NSString stringWithFormat:@"%@?id=%@",iTnuesApi,appid];
//请求更新数据 (这个环节是最耗费时间的环节)
NSData *appData = [NSData dataWithContentsOfURL:[NSURL URLWithString:appURLAPI] options:0 error:&dataError];
if (dataError) {
//NSLog(@"appStore app版本信息请求错误!请重新尝试");
[self showAlertWithMessage:@"appStore app版本信息请求错误!请重新尝试"];
return ;
}
//转换 json 格式
JSON = [NSJSONSerialization JSONObjectWithData:appData options:0 error:nil];
//NSLog(@"ddd : %@",JSON);
//获取appstore 上的app 的版本号
if ([[JSON objectForKey:@"resultCount"] intValue] > 0) {
NSString *remoteVersion = [[[JSON objectForKey:@"results"] objectAtIndex:0] objectForKey:@"version"];
NSString *releaseNotes = [[[JSON objectForKey:@"results"] objectAtIndex:0] objectForKey:@"releaseNotes"];
NSString *trackURL = [[[JSON objectForKey:@"results"] objectAtIndex:0] objectForKey:@"trackViewUrl"];
[[NSUserDefaults standardUserDefaults] setObject:trackURL forKey:@"KK_THE_APP_UPDATE_URL"];
//NSLog(@"%@ %@ %@",remoteVersion,releaseNotes,trackURL);
NSString *localVersion = [self check_LocalApp_Version];
//判断 appstore 的版本是否比本地app 的版本高?如果高就提示更新
if ([remoteVersion floatValue] > [localVersion floatValue]) {
[[CheckVersion check] newVersionUpdate:remoteVersion notes:releaseNotes];
}
else
{
return;
}
}
else
{
//NSLog(@"appStore 无app信息,请检查您的 app id");
[self showAlertWithMessage:@"appStore 无此app信息,请检查您的 app id"];
return ;
}
});
}
+ (void)showAlertWithMessage:(NSString *)messages
{
//返回住线程执行 版本更新提示
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"版本更新提示" message:messages delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
#if !__has_feature(objc_arc)
[alert release];
#endif
});
}
//返回住线程执行 版本更新提示
- (void)newVersionUpdate:(NSString *)version notes:(NSString *)releaseNotes
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"新版本 %@",version] message:releaseNotes delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"更新", nil];
[alert show];
#if !__has_feature(objc_arc)
[alert release];
#endif
});
}
//返回住线程执行 执行更新
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==1) {
//NSString *apiUrl = @"https://itunes.apple.com/us/app/wei-bo/id350962117?mt=8&uo=4";
//apiUrl = @"itms-apps://itunes.apple.com/cn/app/wei-bo/id350962117?mt=8";
NSString *theAppURL = [[NSUserDefaults standardUserDefaults] objectForKey:@"KK_THE_APP_UPDATE_URL"];
NSURL *appStoreURL = [NSURL URLWithString:theAppURL];
[[UIApplication sharedApplication] openURL:appStoreURL];
}
}
本篇 文章参考于 http://www.cnblogs.com/cocoajin/p/3527979.html
- iOS子线程操作检测版本更新,防止阻塞住线程
- iOS子线程操作检测版本更新,有新版本通知用户更新, CheckVersion
- iOS 耗时操作放在子线程,阻止线程阻塞
- iOS子线程操作总结
- Android 子线程操作更新UI方法
- 子线程中更新UI操作
- 主线程就防止在子线程操作UI
- 子线程更新界面
- 子线程更新UI
- 子线程更新UI
- 子线程更新ui
- iOS 检测版本更新
- iOS 检测版本更新
- IOS检测版本更新
- iOS检测版本更新
- iOS 检测版本更新
- iOS 检测版本更新
- IOS 版本更新检测
- 南大软院大神养成计划第九天
- STM8汇编代码分析
- 5-30 Table Tennis (30分)
- 转载的是linuxtone论坛上的前辈学运维的经验,用于共勉。
- Android之Widget认识
- iOS子线程操作检测版本更新,防止阻塞住线程
- 读论语其一
- 欢迎使用CSDN-markdown编辑器
- 浅析Java中的final关键字
- UVa-1225 - Digit Counting:simple
- 在ubuntu中配置jdk安装eclipse
- 顺序表(删除 、查找、插入、合并、输出)
- bfs
- Binary Tree Paths