// // GroupEditerVC.m // smartRhino // // Created by niuzhen on 2020/7/16. // Copyright © 2020 tederen. All rights reserved. // #import "GroupEditerVC.h" #import "_NoInputAccessoryView.h" @interface GroupEditerVC () @property (weak, nonatomic) IBOutlet UIButton *DoneBtn; @property (weak, nonatomic) IBOutlet UIView *NavBarV; @property (nonatomic,strong) WKWebView *webView; @end @implementation GroupEditerVC +(GroupEditerVC *)initGroupEditerVC{ GroupEditerVC *controller = [StoryboardManager.shared.Source instantiateViewControllerWithIdentifier:@"GroupEditerVC"]; return controller; } - (void)viewDidLoad { [super viewDidLoad]; self.fd_prefersNavigationBarHidden = YES; self.view.backgroundColor = UIColorHex(0xF8F8F8); [self.view addSubview:self.webView]; WS(weakSelf); [self.webView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(weakSelf.NavBarV.mas_bottom); make.left.right.mas_equalTo(weakSelf.view); if (@available(iOS 11.0, *)) { make.bottom.equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom); } else { make.bottom.equalTo(weakSelf.view.mas_bottom); } }]; self.webView.UIDelegate = self; // 导航代理 self.webView.navigationDelegate = self; self.webView.scrollView.delegate = self; // self.automaticallyAdjustsScrollViewInsets = YES; // NSURL *pathUrl = [NSURL URLWithString:@"https://apk.tederen.com/mobile/appeditor"]; NSURL *pathUrl = [NSURL URLWithString:HtmlHost(@"/mobile/appeditor")]; NSURLRequest *request = [NSURLRequest requestWithURL:pathUrl]; [self.webView loadRequest:request]; [self.DoneBtn setAction:^{ NSString *jsonString = [NSString stringWithFormat:@"getEditorText()"]; [weakSelf.webView evaluateJavaScript:jsonString completionHandler:^(id _Nullable data, NSError * _Nullable error) { NSLog(@"submit:%@",data); if ([data isKindOfClass:[NSDictionary class]]) { NSString * html = data[@"html"]; if ([data[@"html"] isKindOfClass:[NSNull class]]) { html = @""; } NSDictionary * dict = @{@"Id":@(weakSelf.Id), @"Introduce":html }; SHOWLOADING [[HttpManager sharedHttpManager] POSTUrl:Host(APP_Group_Update) parameters:dict responseStyle:DATA success:^(id _Nonnull responseObject) { REMOVESHOW SHOWSUCCESS(@"编辑成功"); if (weakSelf.UpDateBlock) { weakSelf.UpDateBlock(); } __block BOOL isFound = NO; dispatch_async(dispatch_get_main_queue(), ^{ for (UIViewController * vc in weakSelf.navigationController.viewControllers) { if ([vc isKindOfClass:NSClassFromString(@"GroupSynopsisVC")] ||[vc isKindOfClass:NSClassFromString(@"TDGroupInfoDetailVC")]) { isFound = YES; [[NSNotificationCenter defaultCenter] postNotificationName:GROUPINFOUPDATE object:nil]; [weakSelf.navigationController popToViewController:vc animated:YES]; } } if (!isFound) { [weakSelf.navigationController popViewControllerAnimated:YES]; } }); } failure:^(NSError * _Nonnull error) { REMOVESHOW }]; } }]; }]; } - (NSString *)arrayToJSONString:(NSArray *)array { if (array.count == 0) { return @""; } NSError *error = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:&error]; NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSMutableString *mutStr = [NSMutableString stringWithString:jsonString]; NSRange range = {0,jsonString.length}; //去掉字符串中的空格 [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range]; NSRange range2 = {0,mutStr.length}; //去掉字符串中的换行符 [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2]; return mutStr; } #pragma mark - layload - (WKWebView *)webView { if (!_webView) { WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init]; // 实例化对象 WKUserContentController* userContent = [[WKUserContentController alloc] init]; [userContent addScriptMessageHandler:self name:@"getType"]; configuration.userContentController = userContent; WKPreferences *preferences = [WKPreferences new]; preferences.javaScriptCanOpenWindowsAutomatically = YES; preferences.minimumFontSize = 10.0; configuration.preferences = preferences; // 初始化WKWebView _webView = [[WKWebView alloc]initWithFrame:CGRectZero configuration:configuration]; _NoInputAccessoryView *noInputView = [[_NoInputAccessoryView alloc] init]; [noInputView removeInputAccessoryViewFromWKWebView:_webView]; // UI代理 } return _webView; } #pragma mark - WKScriptMessageHandler - (void)userContentController:(nonnull WKUserContentController *)userContentController didReceiveScriptMessage:(nonnull WKScriptMessage *)message { NSLog(@"messageName:%@",message.name); if ([message.name isEqualToString:@"getType"]) { NSString *jsonString = [NSString stringWithFormat:@"getType('%d','%d','%ld','%@','%d')",99,1,(long)self.Id,USERDEFAULTSGET(@"LOGINTOKEN"),0]; [self.webView evaluateJavaScript:jsonString completionHandler:^(id _Nullable data, NSError * _Nullable error) { }]; } } - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(nonnull NSString *)message initiatedByFrame:(nonnull WKFrameInfo *)frame completionHandler:(nonnull void (^)(void))completionHandler { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message?:@"" preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:([UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }])]; [self presentViewController:alertController animated:YES completion:nil]; NSLog(@"Aleart=========%@",message); completionHandler(); } - (void)dealloc { [self.webView stopLoading]; self.webView.UIDelegate = nil; self.webView.navigationDelegate = nil; self.webView.scrollView.delegate = nil; [self.webView removeFromSuperview]; WKWebsiteDataStore *dateStore = [WKWebsiteDataStore defaultDataStore]; [dateStore fetchDataRecordsOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] completionHandler:^(NSArray * __nonnull records) { for (WKWebsiteDataRecord *record in records) { [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:record.dataTypes forDataRecords:@[record] completionHandler:^{ NSLog(@"Cookies for %@ deleted successfully",record.displayName); }]; } }]; WKUserContentController *userCC = self.webView.configuration.userContentController; [userCC removeScriptMessageHandlerForName:@"getType"]; [userCC removeAllUserScripts]; [[NSNotificationCenter defaultCenter] removeObserver:self]; } @end