您的位置:首页 > 移动开发 > IOS开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: