RQVersionUpdateViewController.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // RQVersionUpdateViewController.m
  3. // SDJK
  4. //
  5. // Created by 张嵘 on 2021/10/11.
  6. //
  7. #import "RQVersionUpdateViewController.h"
  8. @interface RQVersionUpdateViewController ()
  9. /// viewModel
  10. @property (nonatomic, readonly, strong) RQVersionUpdateViewModel *viewModel;
  11. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *topImageToTop;
  12. @property (weak, nonatomic) IBOutlet UILabel *appNameAndVersionLabel;
  13. @property (weak, nonatomic) IBOutlet UILabel *isNewVersionLabel;
  14. @property (weak, nonatomic) IBOutlet UILabel *updateSummaryLabel;
  15. @property (weak, nonatomic) IBOutlet UITextView *noteText;
  16. @property (weak, nonatomic) IBOutlet UIButton *updateBtn;
  17. @property (weak, nonatomic) IBOutlet QMUILabel *dbLabel;
  18. @end
  19. @implementation RQVersionUpdateViewController
  20. @dynamic viewModel;
  21. #pragma mark - SystemMethod
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. /// 初始化
  25. [self rq_setup];
  26. }
  27. #pragma mark - PrivateMethods
  28. /// 初始化
  29. - (void)rq_setup {
  30. /// set up ...
  31. _topImageToTop.constant = RQ_APPLICATION_TOP_BAR_HEIGHT + 48.f;
  32. _appNameAndVersionLabel.text = [NSString stringWithFormat:@"%@|V%@",RQ_APP_DISPLAY_NAME,RQ_APP_VERSION];
  33. _updateBtn.layer.cornerRadius = RQ_FIT_HORIZONTAL(44.f) / 2.f;
  34. _dbLabel.text = [NSString stringWithFormat:@"题库版本|%ld",[RQ_QUESTION_DB_MANAGER getQuestionVersion]];
  35. [self checkVersion];
  36. }
  37. - (void)checkVersion {
  38. NSString *URL = @"http://itunes.apple.com/cn/lookup?id=1586064774";
  39. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URL] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
  40. [request setHTTPMethod:@"POST"];
  41. [MBProgressHUD rq_showProgressHUD:@"检查更新..."];
  42. NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  43. dispatch_async_on_main_queue(^{
  44. [MBProgressHUD rq_hideHUD];
  45. //这个导致部分手机网络不好时候出现闪退情况
  46. if (!data) {
  47. return;
  48. }
  49. //如果data等于nil的时候 再去解析 会发生崩溃
  50. NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
  51. if (!dic) {
  52. return;
  53. }
  54. NSArray *infoArray = [dic objectForKey:@"results"];
  55. if ([infoArray count]) {
  56. NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
  57. NSString *netVersion = [releaseInfo objectForKey:@"version"];
  58. _noteText.text = releaseInfo[@"releaseNotes"]? releaseInfo[@"releaseNotes"] : @"无";
  59. //NSLog(@"AppStore-->%@,手机上-->%@",lastVersion,currentVersion);
  60. // 线上版本号
  61. NSInteger netInt = [[netVersion stringByReplacingOccurrencesOfString:@"." withString:@""] integerValue];
  62. // 本地版本号
  63. NSInteger localInt = [[RQ_APP_VERSION stringByReplacingOccurrencesOfString:@"." withString:@""] integerValue];
  64. if (netInt > localInt) {
  65. _isNewVersionLabel.text = @"发现新版本";
  66. _updateSummaryLabel.text = @"更新摘要";
  67. [_updateBtn setTitleNormal:@"立即更新"];
  68. NSLog(@"更新");
  69. [MBProgressHUD rq_showTips:@"发现新版本"];
  70. }else{
  71. //已是最新版
  72. _isNewVersionLabel.text = @"当前已是最新版";
  73. _updateSummaryLabel.text = @"当前版本更新摘要";
  74. [_updateBtn setTitleNormal:@"检查更新"];
  75. NSLog(@"不更");
  76. [MBProgressHUD rq_showTips:@"当前是最新版了"];
  77. }
  78. } else {
  79. [MBProgressHUD rq_showTips:@"暂无新版本信息"];
  80. }
  81. });
  82. }];
  83. [task resume];
  84. }
  85. - (IBAction)updateBtnAction:(id)sender {
  86. if ([_updateBtn.titleLabel.text isEqualToString:@"检查更新"]) {
  87. [self checkVersion];
  88. } else if ([_updateBtn.titleLabel.text isEqualToString:@"立即更新"]) {
  89. NSURL *url = [NSURL URLWithString:@"itms-apps://itunes.apple.com/cn/app/su-da-jia-kao/id1586064774?l=en&mt=8"];
  90. if (@available(iOS 10.0, *)) {
  91. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  92. } else {
  93. // Fallback on earlier versions
  94. [[UIApplication sharedApplication] openURL:url];
  95. }
  96. }
  97. }
  98. @end