VersionDemo版本更新

之前项目里使用到了版本更新,所以在这里记录下:

//
//  ViewController.m
//  VersionDemo
//
//  Created by zhengll on 16/5/24.
//  Copyright © 2016年 TipTimes-1. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIAlertViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self verSionMethd];
}
-(void) verSionMethd{
    
    NSDictionary * infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString * currentVersion = [infoDic objectForKey:@"FBundleVersion"];
    
    NSString * URL = @"http://itunes.apple.com/cn/lookup?id=APPID";//如果是国内的版本需要加上/cn/
    //APPID  是在AppStore上面的appid
    NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:URL]];
    [request setHTTPMethod:@"POST"];
    NSHTTPURLResponse * urlResponse = nil;
    NSError * error = nil;
    NSData * recerveData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    NSString * results = [[NSString alloc] initWithBytes:[recerveData bytes] length:[recerveData length] encoding:NSUTF8StringEncoding];
    
    NSData * jsonData = [results dataUsingEncoding:NSUTF8StringEncoding];
    
//    NSError * err;
    
    NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    NSArray * infoArray = [dic objectForKey:@"results"];
    if ([infoArray count]) {
        NSDictionary * releaseInfo = [infoArray objectAtIndex:0];
        NSString * lastVersion = [releaseInfo objectForKey:@"version"];
        float last = [lastVersion floatValue];
        float current = [currentVersion floatValue];
        
        if (![lastVersion isEqualToString:currentVersion] && (last > current)) {//在此添加一个判断条件,这样就可以保证,当当前版本低于AppStore版本时进行提示,同时也防止审核不通过,因为现在apple审核不建议添加自定义的版本更新提示
            
   
            NSLog(@"新版本%@,当前版本%@",lastVersion,currentVersion);
            UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"新版本%@已发布!",lastVersion] delegate:self cancelButtonTitle:@"知道了" otherButtonTitles: nil];
            alertView.delegate = self;
            [alertView addButtonWithTitle:@"前往更新"];
            [alertView show];
            alertView.tag = 20;
            
        }else{
            UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:nil message:@"已是最高版本" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
            [alertView show];
            
        }
    }
    
}
-(void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1&& alertView.tag == 20) {
        NSString * url = @"url";//url是app 在 AppStore上面的下载地址
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容