• iOS检查版本更新


    项目原来的检查代码适用于1.2 1.3格式。一般正常的项目格式应该是1.2.2 ,如此大版本.小版本格式。

    贴下代码

    -(void)onCheckVersion
    {
        dispatch_queue_t queue =  dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_async(queue, ^{
            NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
            NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
            NSString *URL = @"http://itunes.apple.com/lookup?id=xxx";
            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];
            NSString *results = [[NSString alloc] initWithBytes:[recervedData bytes] length:[recervedData length] encoding:NSUTF8StringEncoding];
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:[results dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
            NSArray *infoArray = [dic objectForKey:@"results"];
            if ([infoArray count]) {
                NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
                NSString *lastVersion = [releaseInfo objectForKey:@"version"];
                if (([self versionNumberConVersion:lastVersion] >[self versionNumberConVersion:currentVersion])) {
                    alert = [UIAlertController alertControllerWithTitle:@"发现新版本" message:[releaseInfo objectForKey:@"releaseNotes"] preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"暂不" style:UIAlertActionStyleCancel handler:nil];
                    [alert addAction:cancelAction];
                    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    //                    NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/xxname/idxxx?l=en&mt=8"];
                        NSURL *url = [NSURL URLWithString:@"itms-apps://itunes.apple.com/cn/app/xxname/idxxx?mt=8"];
                        [[UIApplication sharedApplication]openURL:url];
                    }];
                    [alert addAction:sureAction];
                    dispatch_async(dispatch_get_main_queue(), ^{
                         [self presentViewController:alert animated:YES completion:nil];
                    });
    
                }
            }
        });
    }
    
    -(int)versionNumberConVersion:(NSString*)version{
        //假如版本号1.5.1 截图如下 这边需要确定几位小数,来确定数组个数。
        int num1 = 0;
        NSArray *ary = [version componentsSeparatedByString:@"."];
        if (ary.count) {
            for (int i = 0; i<ary.count; i++) {
                if (i==0) {
                    num1 = [ary[0] intValue]*10000;
                }else if (i==1){
                    num1 = [ary[0] intValue]*10000+[ary[1] intValue]*100;
                }else if (i==2){
                    num1 = [ary[0] intValue]*10000+[ary[1] intValue]*100+[ary[2] intValue];
                }else if (i>=3){
                    num1 = [ary[0] intValue]*10000+[ary[1] intValue]*100+[ary[2] intValue];
                }
            }
    
        }
        //NSLog(@"版本号%d",num1);
        return num1;
    }
    

      

      

  • 相关阅读:
    Python中的sort()方法使用基础
    centos修改时区shanghai
    const 关键字修饰指针
    多主题
    多语言(国际化)
    动画
    字符编码
    .vscode/extensions.json 是项目用到的 插件 推荐列表,项目应该将此配置 写入用到的插件
    end_of_line = lf 选择行尾序列 .editorconfig 老项目不动代码存盘 文件变动 CRLF 的问题 vscode
    vetur 和 volar 不要一起装 vscode插件 已解决
  • 原文地址:https://www.cnblogs.com/X-Bin/p/5554484.html
Copyright © 2020-2023  润新知