您的位置:首页 > 其它

检查版本是否需要更新

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