RDCheckAppVersion.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // RDCheckAppVersion.m
  3. // SmartHome
  4. //
  5. // Created by Rayson on 2018/3/17.
  6. // Copyright © 2018年 Realde. All rights reserved.
  7. //
  8. #import "RDCheckAppVersion.h"
  9. #import "NSDate+currentDate.h"
  10. #import <UIKit/UIKit.h>
  11. @interface RDCheckAppVersion ()
  12. @property (nonatomic,copy) NSString *appId; // ** appStore的appid */
  13. @end
  14. @implementation RDCheckAppVersion
  15. +(RDCheckAppVersion *)sharedInstance{
  16. static RDCheckAppVersion *instance = nil;
  17. static dispatch_once_t onceToken;
  18. dispatch_once(&onceToken, ^{
  19. instance = [[RDCheckAppVersion alloc] init];
  20. });
  21. return instance;
  22. }
  23. - (NSString *)curVersion{
  24. if (_curVersion == nil) {
  25. _curVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
  26. }
  27. return _curVersion;
  28. }
  29. - (NSString *)curVersionDetail{
  30. if (_curVersionDetail == nil) {
  31. NSString *version = self.curVersion;
  32. NSString *bundle = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
  33. _curVersionDetail = [NSString stringWithFormat:@"%@(%@)",version, bundle];
  34. }
  35. return _curVersionDetail;
  36. }
  37. - (void)checkUpdateAlert{
  38. RDS_WEAKSELF(weakSelf)
  39. [RDSDemoApiHelper rds_appVersionCheckSuccess:^(id responseObject) {
  40. if ([responseObject[@"code"] intValue] == 0) {
  41. NSDictionary *data = responseObject[@"data"];
  42. BOOL needUpdate = [data[@"need_update"] boolValue];
  43. if (!needUpdate) {
  44. [weakSelf noUpdate];
  45. return;
  46. }
  47. NSDictionary *info = data[@"update_info"];
  48. NSString *content = info[@"content"];
  49. BOOL forceUpdate = info[@"force_update"];// 是否强制更新
  50. [weakSelf p_checkUpdateOnAppStoreContent:content canCancel:forceUpdate];
  51. } else{
  52. }
  53. } failure:^(NSError *error) {
  54. }];
  55. }
  56. - (void)noUpdate{
  57. [TheAlertCtrl rds_alertWithTitle:@"提示" message:@"当前是最新版本,无需更新。"];
  58. }
  59. -(void)checkUpdate{
  60. [self p_checkUpdateOnServer];
  61. }
  62. // 请求服务器上的检查更新接口
  63. - (void)p_checkUpdateOnServer{
  64. RDS_WEAKSELF(weakSelf)
  65. [RDSDemoApiHelper rds_appVersionCheckSuccess:^(id responseObject) {
  66. if ([responseObject[@"code"] intValue] == 0) {
  67. NSDictionary *data = responseObject[@"data"];
  68. BOOL needUpdate = [data[@"need_update"] boolValue];
  69. if (!needUpdate) return;
  70. NSDictionary *info = data[@"update_info"];
  71. NSString *content = info[@"content"];
  72. BOOL forceUpdate = info[@"force_update"];// 是否强制更新
  73. [weakSelf p_checkUpdateOnAppStoreContent:content canCancel:forceUpdate];
  74. } else{
  75. }
  76. } failure:^(NSError *error) {
  77. }];
  78. }
  79. - (void)p_checkUpdateOnAppStoreContent:(NSString *)content canCancel:(BOOL)canCancel{
  80. if(canCancel){
  81. // 用户选了暂不更新的话,当天不再提示更新
  82. NSDate *noUpdate = [kUserDefaults objectForKey:@"noUpdate"];
  83. BOOL isToday = [NSDate isDateInToday:noUpdate];
  84. if(isToday) return;
  85. }
  86. NSURL *appUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",kAppIdOnAppStore]];
  87. NSString *appMsg = [NSString stringWithContentsOfURL:appUrl encoding:NSUTF8StringEncoding error:nil];
  88. NSDictionary *appMsgDict = [RDCheckAppVersion jsonStringToDictionary:appMsg];
  89. NSDictionary *appResultsDict = [appMsgDict[@"results"] lastObject];
  90. NSString *appStoreVersion = appResultsDict[@"version"];
  91. // float newVersionFloat = [appStoreVersion floatValue];//新发布的版本号
  92. NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
  93. // float currentVersionFloat = [currentVersion floatValue];//使用中的版本号
  94. NSString *cacheAlertVersion = [kUserDefaults stringForKey:@"appStoreVersion"];
  95. if ([appStoreVersion isEqualToString:cacheAlertVersion] || kNULLString(appStoreVersion)) {
  96. // 商店上相同版本号不再提示
  97. // return ;
  98. }
  99. [kUserDefaults setObject:appStoreVersion forKey:@"appStoreVersion"];
  100. // AppStore 版本号大于当前版本号 (降序)
  101. if ([appStoreVersion compare:currentVersion options:NSNumericSearch] == NSOrderedDescending)
  102. {
  103. self.appId = kAppIdOnAppStore;
  104. self.isShowing = YES;
  105. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"发现新版本\n"
  106. message:content
  107. preferredStyle:UIAlertControllerStyleAlert];
  108. UIAlertAction *done = [UIAlertAction actionWithTitle:@"马上更新"
  109. style:UIAlertActionStyleDefault
  110. handler:^(UIAlertAction * _Nonnull action) {
  111. NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/app/id%@",self.appId]];
  112. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  113. self.isShowing = NO;
  114. }];
  115. [alert addAction:done];
  116. if(canCancel){
  117. UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"暂不更新"
  118. style:UIAlertActionStyleCancel
  119. handler:^(UIAlertAction * action) {
  120. self.isShowing = NO;
  121. [kUserDefaults setObject:[NSDate currentDate] forKey:@"noUpdate"];
  122. }];
  123. [alert addAction:cancel];
  124. }
  125. UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
  126. // 更新UI要在主线程执行
  127. dispatch_async(dispatch_get_main_queue(), ^{
  128. [window.rootViewController presentViewController:alert animated:YES completion:nil];
  129. });
  130. }
  131. }
  132. + (BOOL)isChecking{
  133. NSURL *appUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",kAppIdOnAppStore]];
  134. NSString *appMsg = [NSString stringWithContentsOfURL:appUrl encoding:NSUTF8StringEncoding error:nil];
  135. NSDictionary *appMsgDict = [RDCheckAppVersion jsonStringToDictionary:appMsg];
  136. NSDictionary *appResultsDict = [appMsgDict[@"results"] lastObject];
  137. NSString *appStoreVersion = appResultsDict[@"version"];
  138. // float newVersionFloat = [appStoreVersion floatValue];//新发布的版本号
  139. NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
  140. if (kNULLString(appStoreVersion) || kNULLString(currentVersion)) {
  141. return YES;
  142. }
  143. // 当前 版本号大于AppStore版本号 (降序)
  144. if ([currentVersion compare:appStoreVersion options:NSNumericSearch] == NSOrderedDescending)
  145. {
  146. return YES;
  147. }
  148. else{
  149. return NO;
  150. }
  151. }
  152. + (NSDictionary *)jsonStringToDictionary:(NSString *)jsonStr
  153. {
  154. if (jsonStr == nil)
  155. {
  156. return nil;
  157. }
  158. NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
  159. NSError *error;
  160. NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData
  161. options:NSJSONReadingMutableContainers
  162. error:&error];
  163. if (error)
  164. {
  165. //NSLog(@"json格式string解析失败:%@",error);
  166. return nil;
  167. }
  168. return dict;
  169. }
  170. - (BOOL)isAlertUpdataAgain
  171. {
  172. BOOL res = [[NSUserDefaults standardUserDefaults] objectForKey:@"IS_ALERT_AGAIN"];
  173. return res;
  174. }
  175. @end