iOS子线程操作检测版本更新,防止阻塞住线程
2015-11-25 00:41
411 查看
在做 app 版本更新时发现 请求特别慢,有时甚至等10多秒,严重阻塞主线程,这严重影响了用户体验;
下面介绍一种方法在子线程执行app 版本更新代码;
NSString const *iTnuesApi = @"http://itunes.apple.com/lookup"; //itunes 的链接
//获取本地的版本号
+ (NSString *)check_LocalApp_Version;
{
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
下面介绍一种方法在子线程执行app 版本更新代码;
NSString const *iTnuesApi = @"http://itunes.apple.com/lookup"; //itunes 的链接
//获取本地的版本号
+ (NSString *)check_LocalApp_Version;
{
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 url 编码 解码
- iOS开发CoreAnimation解读之一——初识CoreAnimation核心动画编程
- ios 使用AVPlayer自定义视频播放器
- iOS 实现发送邮件和短信
- IOS开发遇到的绑定控件的问题
- 转 ios 里如何判断当前应用的定位服务是否可用
- iOS开发多线程-线程间的通信
- iOS ViewController之间传值方法五(使用block)
- IOS 实现不定参数方法
- IOS开发真机调试
- iOS-GCD多线程之dispatch_once
- iOS开发多线程-线程安全
- IOS开发打电话发短信发邮件
- ios 从系统相册选择多张图片
- iOS跳转到指定页面,而不是简单的返回上一个页面
- 图形变换之使用坐标变换
- iOS视图生命周期
- iOS单例的两种实现
- ios中nsdate与实际时间相隔八小时解决方案
- 【IOS】AutoLayout框架Masonry