iOS APP检查版本更新

2016-10-27  本文已影响654人  carpond

在开发中,我们可能会遇到这样的需求,当AppStore中有新版本需要更新迭代,用户在点开APP的时候弹出提示框提醒用户去AppStore更新。
这里需要我们判断当前APP与AppStore中的版本差别,如果一样,不需要提示;如果不一样就弹出提示框,给用户提示更新APP版本。

-(void)viewDidLoad{
    [super viewDidLoad];
    //iOS应用版本检测
    [self versionDetection];
}
-(void)versionDetection{
    //获取当前发布的版本的Version
    NSString *currentVersionStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://itunes.apple.com/cn/lookup?id=1061858098"] encoding:NSUTF8StringEncoding error:nil];
    //判断字符串是否为nil,长度是不是大于0,以及是不是取得版本
    if (currentVersionStr != nil && [currentVersionStr length] > 0 && [currentVersionStr rangeOfString:@"version"].length == 7) {
        [self checkAppUpdate:currentVersionStr];
    }
}

代码中的url是每个APP的链接,其中id可以在下图中查到,这个id是上线后自动分配的唯一id,这个id还有其他的查看方式,这里就不说了。由于公司app的销售范围是国内,所以链接是http://itunes.apple.com/cn/lookup?id=1061858098,如果销售范围不只国内链接需改成http://itunes.apple.com/lookup?id=1061858098,否则拿到的results会为空

图1.png
-(void)checkAppUpdate:(NSString *)currentVersionStr{
    //string转dictionary
    NSDictionary *versionDict = [global dictionaryWithJSonString:currentVersionStr];
    //获取当前版本
    NSString *versionStr = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
    //线上App版本
    NSString *appVersion = [currentVersionStr substringFromIndex:[currentVersionStr rangeOfString:@"\"version\":"].location+10];
    appVersion = [[appVersion substringToIndex:[appVersion rangeOfString:@","].location] stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    NSArray *localArray = [versionStr componentsSeparatedByString:@"."];
    NSArray *versionArray = [appVersion componentsSeparatedByString:@"."];
    //这里比较版本号不是不一样就提示更新升级。而是当前版本号如果比AppStore版备号小的时候提示弹框升级。这样做的最大好处就是苹果在审核App时不会出现提示升级。
    if ((versionArray.count == 3) && (localArray.count == versionArray.count)) {
        if ([localArray[0] intValue] <  [versionArray[0] intValue]) {
            [self uodateVersion:currentVersionStr];
        }else if ([localArray[0] intValue]  ==  [versionArray[0] intValue]){
            if ([localArray[1] intValue] <  [versionArray[1] intValue]) {
               [self uodateVersion:currentVersionStr];
            }else if ([localArray[1] intValue] ==  [versionArray[1] intValue]){
                if ([localArray[2] intValue] <  [versionArray[2] intValue]) {
                    [self uodateVersion:currentVersionStr];
                }
            }
        }
    }
-(void)uodateVersion:(NSString *)currentVersion {
        NSDictionary *versionDict = [global dictionaryWithJSonString:currentVersion];
        NSString * releaseNotesString= [[[versionDict objectForKey:@"results"] objectAtIndex:0] valueForKey:@"releaseNotes"];
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"发现新版本" message:releaseNotesString preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSString *url = @"https://itunes.apple.com/cn/app/de-wei-dian-shang/id1061858098?mt=8&uo=4";
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
        }];
        [alertController addAction:cancelAction];
        [alertController addAction:okAction];
        [self presentViewController:alertController animated:YES completion:nil];
}

Demo

纠正,现在这种方式会被苹果给拒绝,可以换成后台控制这个弹出框的显示
结语:如有错误请留言!
如果疑问请留言,一起进步

上一篇 下一篇

猜你喜欢

热点阅读