Browse Source

版本更新逻辑优化。

Steven 2 tháng trước cách đây
mục cha
commit
8fca3307e1

+ 44 - 32
KulexiuForTeacher/KulexiuForTeacher/AppDelegate.m

@@ -76,6 +76,11 @@
     [KSNetworkAccessibleManager setAlertEnable:YES];
     [KSNetworkAccessibleManager setStateDidUpdateNotifier:^(KSNetworkAccessibleState state) {
         NSLog(@"setStateDidUpdateNotifier > %zd", state);
+        if (state == KSNetworkAccessible) {
+            if (self.isNeedUpdate) {
+                [self versionCheck];
+            }
+        }
     }];
     
     [KSNetworkAccessibleManager start];
@@ -236,39 +241,46 @@
 
 - (void)versionCheck {
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-        [KSUpdateManager ks_updateWithAPPID:@"1626971149" withBundleId:nil block:^(NSString * _Nonnull currentVersion, NSString * _Nonnull storeVersion, NSString * _Nonnull openUrl, BOOL isUpdate) {
-            if (isUpdate) {
-                
-                [KSNetworkingManager appVersionInfoRequest:KS_GET success:^(NSDictionary * _Nonnull dic) {
-                    if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
-
-                        NSString *serviceVersion = [[dic ks_dictionaryValueForKey:@"data"] ks_stringValueForKey:@"version"];
-                        if ([serviceVersion isEqualToString:storeVersion] || [self isLowerVersionCompareLocalVersion:currentVersion serviceVersion:serviceVersion]) {
-                            // desc
-                            NSString *descMessage = [[dic ks_dictionaryValueForKey:@"data"] ks_stringValueForKey:@"description"];
-                            NSString *downloadUrl = [[dic ks_dictionaryValueForKey:@"data"] ks_stringValueForKey:@"downloadUrl"];
-                            // 如果没有配置
-                            if ([NSString isEmptyString:downloadUrl]) {
-                                downloadUrl = openUrl;
-                            }
-                            // 判断
-                            if ([[dic ks_dictionaryValueForKey:@"data"] ks_boolValueForKey:@"isForceUpdate"]) {
-                                self.isNeedUpdate = YES;
-                                [self showAlertWithMemo:storeVersion descMessage:descMessage isforce:YES openUrl:downloadUrl];
-                            }
-                            else {
-                                self.isNeedUpdate = NO;
-                                [self showAlertWithMemo:storeVersion descMessage:descMessage isforce:NO openUrl:downloadUrl];
+        [KSUpdateManager ks_updateWithAPPID:@"1626971149" withBundleId:nil block:^(BOOL isSuccess, NSString * _Nonnull currentVersion, NSString * _Nonnull storeVersion, NSString * _Nonnull openUrl, BOOL isUpdate) {
+            if (isSuccess) {
+                if (isUpdate) {
+                    
+                    [KSNetworkingManager appVersionInfoRequest:KS_GET success:^(NSDictionary * _Nonnull dic) {
+                        if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
+
+                            NSString *serviceVersion = [[dic ks_dictionaryValueForKey:@"data"] ks_stringValueForKey:@"version"];
+                            if ([serviceVersion isEqualToString:storeVersion] || [self isLowerVersionCompareLocalVersion:currentVersion serviceVersion:serviceVersion]) {
+                                // desc
+                                NSString *descMessage = [[dic ks_dictionaryValueForKey:@"data"] ks_stringValueForKey:@"description"];
+                                NSString *downloadUrl = [[dic ks_dictionaryValueForKey:@"data"] ks_stringValueForKey:@"downloadUrl"];
+                                // 如果没有配置
+                                if ([NSString isEmptyString:downloadUrl]) {
+                                    downloadUrl = openUrl;
+                                }
+                                // 判断
+                                if ([[dic ks_dictionaryValueForKey:@"data"] ks_boolValueForKey:@"isForceUpdate"]) {
+                                    self.isNeedUpdate = YES;
+                                    [self showAlertWithMemo:storeVersion descMessage:descMessage isforce:YES openUrl:downloadUrl];
+                                }
+                                else {
+                                    self.isNeedUpdate = NO;
+                                    [self showAlertWithMemo:storeVersion descMessage:descMessage isforce:NO openUrl:downloadUrl];
+                                }
                             }
-                        }
 
-                    }
-                } faliure:^(NSError * _Nonnull error) {
-                    [self showAlertWithMemo:storeVersion descMessage:@"全新版本上线了,赶快来体验吧!" isforce:NO openUrl:openUrl];
-                }];
+                        }
+                    } faliure:^(NSError * _Nonnull error) {
+                        if ([self isLowerVersionCompareLocalVersion:currentVersion serviceVersion:storeVersion]) {
+                            [self showAlertWithMemo:storeVersion descMessage:@"全新版本上线了,赶快来体验吧!" isforce:NO openUrl:openUrl];
+                        }
+                    }];
+                }
+                else {
+                    NSLog(@"当前版本%@,商店版本%@,不需要更新",currentVersion,storeVersion);
+                }
             }
             else {
-                NSLog(@"当前版本%@,商店版本%@,不需要更新",currentVersion,storeVersion);
+                self.isNeedUpdate = YES;
             }
         }];
     });
@@ -441,9 +453,6 @@ didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
         [self.timer invalidate];
         self.timer = nil;
     }
-    if (self.isNeedUpdate) {
-        [self versionCheck];
-    }
     [self checkConnectionStatus];
     
     [[NSNotificationCenter defaultCenter] postNotificationName:@"appBecomeActive" object:nil];
@@ -463,6 +472,9 @@ didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
 - (void)applicationDidBecomeActive:(UIApplication *)application {
     // app启动或者app从后台进入前台都会调用这个方法
     [JPUSHService resetBadge]; // 清空 JPush 服务器中存储的 badge 值
+    if (self.isNeedUpdate) {
+        [self versionCheck];
+    }
 }
 
 

+ 1 - 1
KulexiuForTeacher/KulexiuForTeacher/Common/Base/AlertView/KSUpdateManager.h

@@ -20,7 +20,7 @@ NS_ASSUME_NONNULL_BEGIN
  @param openUrl 跳转的地址
  @param isUpdate 是否为最新版本
  */
-typedef void(^UpdateBlock)(NSString *currentVersion,NSString *storeVersion, NSString *openUrl,BOOL isUpdate);
+typedef void(^UpdateBlock)(BOOL isSuccess, NSString *currentVersion, NSString *storeVersion, NSString *openUrl, BOOL isUpdate);
 
 
 /**

+ 4 - 4
KulexiuForTeacher/KulexiuForTeacher/Common/Base/AlertView/KSUpdateManager.m

@@ -46,7 +46,7 @@
         if (error) {
             NSLog(@"【3】检测失败,原因:\n%@",error);
             dispatch_async(dispatch_get_main_queue(), ^{
-                block(currentVersion,@"",@"",NO);
+                block(NO, currentVersion, @"", @"", NO);
             });
             return;
         }
@@ -54,7 +54,7 @@
             NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
             if ([appInfoDic[@"resultCount"] integerValue] == 0) {
                 NSLog(@"检测出未上架的APP或者查询不到");
-                block(currentVersion,@"",@"",NO);
+                block(NO, currentVersion, @"", @"", NO);
                 return;
             }
             NSLog(@"【3】苹果服务器返回的检测结果:\n appId = %@ \n bundleId = %@ \n 开发账号名字 = %@ \n 商店版本号 = %@ \n 应用名称 = %@ \n 打开连接 = %@",appInfoDic[@"results"][0][@"artistId"],appInfoDic[@"results"][0][@"bundleId"],appInfoDic[@"results"][0][@"artistName"],appInfoDic[@"results"][0][@"version"],appInfoDic[@"results"][0][@"trackName"],appInfoDic[@"results"][0][@"trackViewUrl"]);
@@ -80,10 +80,10 @@
             if([currentVersion floatValue] < [appStoreVersion floatValue])
             {
                 NSLog(@"【4】判断结果:当前版本号%@ < 商店版本号%@ 需要更新\n=========我是分割线========",[[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"],appInfoDic[@"results"][0][@"version"]);
-                block(currentVersion,appInfoDic[@"results"][0][@"version"],appInfoDic[@"results"][0][@"trackViewUrl"],YES);
+                block(YES, currentVersion, appInfoDic[@"results"][0][@"version"], appInfoDic[@"results"][0][@"trackViewUrl"], YES);
             }else{
                 NSLog(@"【4】判断结果:当前版本号%@ > 商店版本号%@ 不需要更新\n========我是分割线========",[[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"],appInfoDic[@"results"][0][@"version"]);
-                block(currentVersion,appInfoDic[@"results"][0][@"version"],appInfoDic[@"results"][0][@"trackViewUrl"],NO);
+                block(YES, currentVersion, appInfoDic[@"results"][0][@"version"], appInfoDic[@"results"][0][@"trackViewUrl"], NO);
             }
         });
     }];