// // LaunchViewController.m // ShenQi // // Created by Yao on 2024/12/19. // #import "LaunchViewController.h" #import #import "AppDelegate.h" #import "MacroDefine.h" #import "ConfigData.h" #import "ConfigDataModel.h" #import "MainOldViewController.h" @interface LaunchViewController () @property (nonatomic, strong) NSString *mainUserId; @property (nonatomic, strong) NSString *mainURLString; @end @implementation LaunchViewController - (void)viewDidLoad { [super viewDidLoad]; self.mainUserId = NowUserId; [self setupGameURLData]; } -(void)intoMainWebView:(NSString *)backupDomains { //主域名不行用备用域名 NSArray *textArr = [backupDomains componentsSeparatedByString:@","]; [self getAccessibleDomain:self.mainURLString backupDomains:textArr completion:^(NSString *accessibleDomain) { dispatch_async(dispatch_get_main_queue(), ^{ self.mainURLString = accessibleDomain; UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; MainOldViewController *webViewController = [sb instantiateViewControllerWithIdentifier:@"MainOldViewController"]; webViewController.mainURLString = self.mainURLString; AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; appDelegate.mainURLString = self.mainURLString; appDelegate.window.rootViewController = webViewController; }); }]; } -(void)setupGameURLData { NSString *urlstring = [NSString stringWithFormat:@"%@?userId=%@",ConfigURL,self.mainUserId]; NSURL *url = [NSURL URLWithString:urlstring]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"GET"; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { dispatch_async(dispatch_get_main_queue(), ^{ if (error) { [SVProgressHUD showInfoWithStatus:error.localizedDescription]; }else{ NSError *error; NSMutableDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; if (error) { [SVProgressHUD showInfoWithStatus:error.localizedDescription]; }else{ if (ISNULL(error) && !ISNULL(responseObject) && [[responseObject objectForKey:@"code"] integerValue] == 1) { NSDictionary *datadic = [responseObject objectForKey:@"data"]; ConfigDataModel *model = [[ConfigDataModel alloc] initWithDictionary:responseObject]; AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; appDelegate.data = model.data; if (model.data.isUse == 1) { if (model.data.contactApplyMode == 1 || model.data.noticeApplyMode == 1) { [appDelegate applicationDidBecomeActive:nil]; } // 判断是否有新版本 [self compareVersionWithDictionary:datadic]; self.mainURLString = [datadic objectForKey:@"url"]; self.mainURLString = [self.mainURLString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; NSString *backupDomains = [datadic objectForKey:@"backupDomains"]; if (!ISNULLSTR(self.mainURLString)) { [self intoMainWebView:backupDomains]; } }else{ dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController *alertview = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Tips", nil) message:NSLocalizedString(@"App Can not Use", nil) preferredStyle:(UIAlertControllerStyleAlert)]; UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) { exit(0); }]; [alertview addAction:action]; [self presentViewController:alertview animated:YES completion:^{}]; }); } } } } }); }]; [dataTask resume]; } -(void)compareVersionWithDictionary:(NSDictionary *)datadic { AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; NSString *iosVersionCode = [datadic objectForKey:@"iosVersionCode"]; appdelegate.downloadUrl = [datadic objectForKey:@"downloadUrl"]; NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary]; NSString *localVison = infoDict[@"CFBundleShortVersionString"]; if ([localVison compare:iosVersionCode] == NSOrderedAscending) { appdelegate.forceUpdate = [[datadic objectForKey:@"forceUpdate"] boolValue]; UIAlertController *alertview = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Tips", nil) message:NSLocalizedString(@"New version found", nil) preferredStyle:(UIAlertControllerStyleAlert)]; UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"Update", nil) style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) { appdelegate.isRefresh = NO; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appdelegate.downloadUrl] options:@{} completionHandler:^(BOOL success) {}]; }]; [alertview addAction:action]; if (appdelegate.forceUpdate == NO) { UIAlertAction *cancelaction = [UIAlertAction actionWithTitle:NSLocalizedString(@"CANCEL", nil) style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {}]; [alertview addAction:cancelaction]; } dispatch_async(dispatch_get_main_queue(), ^{ [self presentViewController:alertview animated:YES completion:^{}]; }); } } - (void)checkDomainAccessibility:(NSString *)domain completion:(void(^)(BOOL isAccessible))completion { NSString *urlString = domain; NSURL *url = [NSURL URLWithString:urlString]; NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { BOOL isAccessible = NO; if (error) { // 请求失败 NSLog(@"Domain %@ is not reachable: %@", domain, error.localizedDescription); } else { // 请求成功,检查返回的状态码 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; if (httpResponse.statusCode == 200) { isAccessible = YES; } else { NSLog(@"Domain %@ returned status code: %ld", domain, (long)httpResponse.statusCode); } } // 回调,返回访问状态 if (completion) { completion(isAccessible); } }]; [task resume]; } #pragma mark - 主域名不行用备用域名 - (void)getAccessibleDomain:(NSString *)primaryDomain backupDomains:(NSArray *)backupDomains completion:(void(^)(NSString *accessibleDomain))completion { if (ISNULLARRAY(backupDomains)) { if (completion) { completion(primaryDomain); } return; } // 首先检查主域名 [self checkDomainAccessibility:primaryDomain completion:^(BOOL isAccessible) { if (isAccessible) { // 如果主域名可访问,直接返回主域名 if (completion) { completion(primaryDomain); } } else { // 主域名不可访问,遍历备用域名 __block NSString *reachableDomain = nil; dispatch_group_t group = dispatch_group_create(); // 遍历备用域名进行检查 for (NSString *backupDomain in backupDomains) { dispatch_group_enter(group); [self checkDomainAccessibility:backupDomain completion:^(BOOL isAccessible) { if (isAccessible && !reachableDomain) { reachableDomain = backupDomain; } dispatch_group_leave(group); }]; } // 等待所有备用域名的检查完成 dispatch_group_notify(group, dispatch_get_main_queue(), ^{ // 如果找到了可访问的备用域名,返回它 if (reachableDomain) { if (completion) { completion(reachableDomain); } } else { // 如果所有域名都不可访问,返回主域名 if (completion) { completion(primaryDomain); } } }); } }]; } @end