// // RQVersionUpdateViewController.m // SDJK // // Created by 张嵘 on 2021/10/11. // #import "RQVersionUpdateViewController.h" @interface RQVersionUpdateViewController () /// viewModel @property (nonatomic, readonly, strong) RQVersionUpdateViewModel *viewModel; @property (weak, nonatomic) IBOutlet NSLayoutConstraint *topImageToTop; @property (weak, nonatomic) IBOutlet UILabel *appNameAndVersionLabel; @property (weak, nonatomic) IBOutlet UILabel *isNewVersionLabel; @property (weak, nonatomic) IBOutlet UILabel *updateSummaryLabel; @property (weak, nonatomic) IBOutlet UITextView *noteText; @property (weak, nonatomic) IBOutlet UIButton *updateBtn; @property (weak, nonatomic) IBOutlet QMUILabel *dbLabel; @end @implementation RQVersionUpdateViewController @dynamic viewModel; #pragma mark - SystemMethod - (void)viewDidLoad { [super viewDidLoad]; /// 初始化 [self rq_setup]; } #pragma mark - PrivateMethods /// 初始化 - (void)rq_setup { /// set up ... _topImageToTop.constant = RQ_APPLICATION_TOP_BAR_HEIGHT + 48.f; _appNameAndVersionLabel.text = [NSString stringWithFormat:@"%@|V%@",RQ_APP_DISPLAY_NAME,RQ_APP_VERSION]; _updateBtn.layer.cornerRadius = RQ_FIT_HORIZONTAL(44.f) / 2.f; _dbLabel.text = [NSString stringWithFormat:@"题库版本|%ld",[RQ_QUESTION_DB_MANAGER getQuestionVersion]]; [self checkVersion]; } - (void)checkVersion { NSString *URL = @"http://itunes.apple.com/cn/lookup?id=1586064774"; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URL] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10]; [request setHTTPMethod:@"POST"]; [MBProgressHUD rq_showProgressHUD:@"检查更新..."]; NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { dispatch_async_on_main_queue(^{ [MBProgressHUD rq_hideHUD]; //这个导致部分手机网络不好时候出现闪退情况 if (!data) { return; } //如果data等于nil的时候 再去解析 会发生崩溃 NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; if (!dic) { return; } NSArray *infoArray = [dic objectForKey:@"results"]; if ([infoArray count]) { NSDictionary *releaseInfo = [infoArray objectAtIndex:0]; NSString *netVersion = [releaseInfo objectForKey:@"version"]; _noteText.text = releaseInfo[@"releaseNotes"]? releaseInfo[@"releaseNotes"] : @"无"; //NSLog(@"AppStore-->%@,手机上-->%@",lastVersion,currentVersion); // 线上版本号 NSInteger netInt = [[netVersion stringByReplacingOccurrencesOfString:@"." withString:@""] integerValue]; // 本地版本号 NSInteger localInt = [[RQ_APP_VERSION stringByReplacingOccurrencesOfString:@"." withString:@""] integerValue]; if (netInt > localInt) { _isNewVersionLabel.text = @"发现新版本"; _updateSummaryLabel.text = @"更新摘要"; [_updateBtn setTitleNormal:@"立即更新"]; NSLog(@"更新"); [MBProgressHUD rq_showTips:@"发现新版本"]; }else{ //已是最新版 _isNewVersionLabel.text = @"当前已是最新版"; _updateSummaryLabel.text = @"当前版本更新摘要"; [_updateBtn setTitleNormal:@"检查更新"]; NSLog(@"不更"); [MBProgressHUD rq_showTips:@"当前是最新版了"]; } } else { [MBProgressHUD rq_showTips:@"暂无新版本信息"]; } }); }]; [task resume]; } - (IBAction)updateBtnAction:(id)sender { if ([_updateBtn.titleLabel.text isEqualToString:@"检查更新"]) { [self checkVersion]; } else if ([_updateBtn.titleLabel.text isEqualToString:@"立即更新"]) { NSURL *url = [NSURL URLWithString:@"itms-apps://itunes.apple.com/cn/app/su-da-jia-kao/id1586064774?l=en&mt=8"]; if (@available(iOS 10.0, *)) { [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil]; } else { // Fallback on earlier versions [[UIApplication sharedApplication] openURL:url]; } } } @end