iOS子线程操作检测版本更新,有新版本通知用户更新, CheckVersion
2015-07-16 14:50
453 查看
iOS子线程操作检测版本更新,有新版本通知用户更新 CheckVersion
一:如何使用:
上述代码写完就可以了,当用户打开app检测到新版本时,为通知用户,更新,并显示最新版本的更新内容;
二:CheckVersion 类
原文链接:http://www.cnblogs.com/cocoajin/p/3527979.html?utm_source=tuicool
一:如何使用:
#import "CheckVersion.h" //输入你的app在appStore的 id [CheckVersion check_APP_UPDATE_WITH_APPID:@"350962117"];
上述代码写完就可以了,当用户打开app检测到新版本时,为通知用户,更新,并显示最新版本的更新内容;
二:CheckVersion 类
// // CheckVersion.h // TopProgressView // // Created by cocoajin on 14-1-20. // Copyright (c) 2014年 www.zhgu.net. All rights reserved. // #import <Foundation/Foundation.h> extern NSString const *iTnuesApi; @interface CheckVersion : NSObject //+ (instancetype)check; + (NSString *)check_LocalApp_Version; + (void )check_APP_UPDATE_WITH_APPID:(NSString *)appid; @end
// // CheckVersion.m // TopProgressView // // Created by cocoajin on 14-1-20. // Copyright (c) 2014年 www.zhgu.net. All rights reserved. // #import "CheckVersion.h" NSString const *iTnuesApi = @"http://itunes.apple.com/lookup"; #define kTestApp @"http://itunes.apple.com/lookup?id=350962117" //新浪微博 app测试 @implementation CheckVersion + (instancetype)check { static CheckVersion *check = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ check = [[CheckVersion alloc]init]; }); return check; } + (NSString *)check_LocalApp_Version; { NSString *localVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]; return localVersion; } + (void )check_APP_UPDATE_WITH_APPID:(NSString *)appid { __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 = [NSJSONSerialization JSONObjectWithData:appData options:0 error:nil]; //NSLog(@"ddd : %@",JSON); 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]; 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]; } } @end
原文链接:http://www.cnblogs.com/cocoajin/p/3527979.html?utm_source=tuicool
相关文章推荐
- iOS 纯代码适配iPhone6,6+
- iOS发展- 文件共享(使用iTunes导入文件, 并显示现有文件)
- IOS 委托和协议区别和联系 (-)
- IOS 委托和协议区别和联系 (-)
- PushMeBaby 使用
- XCode 6 的 iOS 免证书真机测试方法
- IOS Block很简单,就像delegate的简化版
- iOS 8 Xcode6 设置Launch Image 启动图片
- iOS Icon 规范
- ios学习笔记--(c基础题1)
- iOS深入学习(创建静态库static library)
- IOS:Camera的特性分析与使用3_OPENGL特效
- iOS开发中存储数据的方法
- ios射击类游戏简单代码射击
- 不推荐在iOS的浏览器应用上使用click和mouseover
- 项目开发之插件的安装 分类: ios开发 2015-07-16 13:56 183人阅读 评论(0) 收藏
- 【iOS知识学习】_iPhone学习四舍五入函数
- ios实现一个射击游戏,逻辑分析与对象的提取
- [iOS]生成并使用静态库(.a)文件
- iOS GCD实现发送验证码倒计时