// // 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 = Candy916UserID; [self setupGameURLData]; } -(void)intoMainWebView { 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]]; if (!ISNULLSTR(self.mainURLString)) { [self intoMainWebView]; } }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:^{}]; }); } } @end