检查版本是否需要更新
2015-09-30 16:34
513 查看
挺简单的方法,直接上代码。
// // ViewController.m // 检测版本更新,并提示用户更新 // // Created by 鲁杜杨 on 15/9/29. // Copyright © 2015年 鲁杜杨. All rights reserved. // #import "ViewController.h" @interface ViewController () @property(nonatomic,retain) UIButton *kCheckBtn; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.kCheckBtn]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } -(void)checkVersion { NSString *currentVersion=[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]; [self method1WithCurrentVersion:currentVersion]; } //根据应用程序名称来进行模糊搜索 -(void)method1WithCurrentVersion:(NSString *)currentVersion { NSString *urlStr=@"http://itunes.apple.com/search?term=QQ&entity=software"; NSMutableURLRequest *request=[[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:urlStr]]; [request setHTTPMethod:@"POST"]; NSHTTPURLResponse *urlReponse=nil; NSError *error=nil; NSData *recerveData=[NSURLConnection sendSynchronousRequest:request returningResponse:&urlReponse error:&error]; NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:recerveData options:NSJSONReadingMutableContainers error:nil]; //应用所有版本(包括曾经的版本)信息数组 NSArray *results=[dict objectForKey:@"results"]; //最新版本的信息 NSDictionary *appInfo=[results objectAtIndex:0]; // NSLog(@"appInfo=%@",appInfo); //版本号 NSString *version=[appInfo objectForKey:@"version"]; //下载地址 NSString *trackViewUrl=[appInfo objectForKey:@"trackViewUrl"]; if (![currentVersion isEqualToString:version]) { UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"有新版本" message:trackViewUrl delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil]; [alertView show]; } } //根据应用程序名称的ID进行精确搜索 -(void)method2WithCurrentVersion:(NSString *)currentVersion { NSString *urlStr=@"http://itunes.apple.com/lookup?id=你的应用程序的ID"; NSMutableURLRequest *request=[[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:urlStr]]; [request setHTTPMethod:@"POST"]; NSHTTPURLResponse *urlReponse=nil; NSError *error=nil; NSData *recerveData=[NSURLConnection sendSynchronousRequest:request returningResponse:&urlReponse error:&error]; NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:recerveData options:NSJSONReadingMutableContainers error:nil]; } -(UIButton *)kCheckBtn { if (_kCheckBtn==nil) { _kCheckBtn=[UIButton buttonWithType:UIButtonTypeCustom]; _kCheckBtn.frame=CGRectMake(100, 100, 200, 40); [_kCheckBtn setBackgroundColor:[UIColor cyanColor]]; [_kCheckBtn setTitle:@"检查版本" forState:UIControlStateNormal]; [_kCheckBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal]; [_kCheckBtn addTarget:self action:@selector(checkVersion) forControlEvents:UIControlEventTouchUpInside]; } return _kCheckBtn; } @end
相关文章推荐
- perl 连接oracle 配置
- 解决数据库时间字段无法被描述
- 设置动画从头部出去
- 论文笔记(1)DenseBox: Unifying Landmark Localization with End to End Object Detection
- 经典算法题——第三题 猴子吃桃
- c笔记10---多文件操作,Makefile,结构体 struct 及结构体变量,重命名 typedef
- 微信里面防止下拉"露底"组件
- 安装VMM 2012 R2累积更新1可能遇到的问题
- 我的第一篇CSDN博客
- Android Studio如何更改JDK和SDK的路径
- property tree修改xml文件内容
- iOS中使用模板引擎渲染HTML界面
- mysql结合status和variables 优化mysql
- c笔记09---字符数组及字符串,预处理,宏,条件编译
- VirtualBox centos下设置共享文件夹
- 通过代码动态添加列表item实现列表(不使用listview列表)
- 通过同义词获取同义词对应表的列名和列的类型
- opencv访问图像中每个像素的值
- Substring with Concatenation of All Words
- Linux下查看软、硬raid信息的方法