app版本自动更新-iOS忙里偷闲整理系列

原理啊,其实很简单,就是比较app的当前版本号和服务器上的版本号是否一样,不一样就提示更新。

iOS程序自动提示更新的实现方案大致分为两种:
第一种,自己服务器提供一个接口,告知相关app的当前版本,是否需要更新,以及更新的地址等信息 。
第二种,就是利用苹果的appStore 提供的相关api进行查询更新。

代码如下:

#define APPUrl @"http://itunes.apple.com/lookup?id=你应用ID 在itunes上的"

// 检查是否更新
-(void) onCheckVersion
{
   //  1.获取当前版本号
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    //CFShow((__bridge CFTypeRef)(infoDic));
    NSString *currentVersion = [infoDic objectForKey:@"CFBundleVersion"];
    
    NSString *URL = APPUrl;
    // 2.发请求 给苹果的api 获取app的info
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:URL]];
    [request setHTTPMethod:@"POST"];
    NSHTTPURLResponse *urlResponse = nil;
    NSError *error = nil;
    NSData *recervedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    NSDictionary *appInofDic=[NSJSONSerialization JSONObjectWithData:recervedData options:NSJSONReadingMutableLeaves error:&error];
    if (error) {
        NSLog(@"error:%@",[error description]);
    }
    NSArray *resultArray=[appInofDic objectForKey:@"results"];
    if (![resultArray count]) {
        NSLog(@"error:results==nil");
    }
    
    if ([resultArray count]>0)
    {
        NSDictionary *infoDics=[resultArray objectAtIndex:0];
     // 获取appstore最新的版本号 NSString *latestVersion=[infoDics objectForKey:@"version"];
     // 获取应用程序的地址  _trackViewUrl=[infoDics objectForKey:@"trackViewUrl"]; NSString *trackName=[infoDics objectForKey:@"trackName"]; double doubleCurrentVersion=[currentVersion doubleValue]; double doubleUpdateVersion=[latestVersion doubleValue]; if (doubleCurrentVersion<doubleUpdateVersion) { NSString *titleStr=[NSString stringWithFormat:@"检查更新:%@",trackName]; NSString *messageStr=[NSString stringWithFormat:@"发现新版本(%@),是否升级?",latestVersion]; UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:titleStr message:messageStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"升级", nil]; alertView.tag=1; [alertView show]; }else{ NSString *titleStr=[NSString stringWithFormat:@"检查更新:%@",trackName]; UIAlertView *alertviews=[[UIAlertView alloc]initWithTitle:titleStr message:@"暂无新版本" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; alertviews.tag=2; [alertviews show]; } }else { UIAlertView *alertviews=[[UIAlertView alloc]initWithTitle:@"通知"message:@"暂无新版本" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; alertviews.tag=3; [alertviews show]; } } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex==1) { [SaveUserInfo saveUserInfo:nil]; [self.tableView reloadData]; } if (alertView.tag==1) { if (buttonIndex==1) { //如果有新的版本,那么就跳转AppStore [ [UIApplication sharedApplication]openURL:[NSURL URLWithString:_trackViewUrl]]; } } }

  注意 :

1.应用ID  在这里获取(上架app的地方)

 

2.容易搞混淆的

info.plist文件里的“Bundle version”字段,供程序调试用,即内部调试版本号,不是显示在appstore上面的,version 对应info.plist文件里的“Bundle versions string, short”字段,这才是真正的版本号,显示在appstore上的。



posted @ 2015-11-12 00:09  timo1234  阅读(465)  评论(0编辑  收藏  举报