GroupEditerVC.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // GroupEditerVC.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2020/7/16.
  6. // Copyright © 2020 tederen. All rights reserved.
  7. //
  8. #import "GroupEditerVC.h"
  9. #import "_NoInputAccessoryView.h"
  10. @interface GroupEditerVC ()<WKUIDelegate,WKNavigationDelegate,UIScrollViewDelegate,WKScriptMessageHandler>
  11. @property (weak, nonatomic) IBOutlet UIButton *DoneBtn;
  12. @property (weak, nonatomic) IBOutlet UIView *NavBarV;
  13. @property (nonatomic,strong) WKWebView *webView;
  14. @end
  15. @implementation GroupEditerVC
  16. +(GroupEditerVC *)initGroupEditerVC{
  17. GroupEditerVC *controller = [StoryboardManager.shared.Source instantiateViewControllerWithIdentifier:@"GroupEditerVC"];
  18. return controller;
  19. }
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. self.fd_prefersNavigationBarHidden = YES;
  23. self.view.backgroundColor = UIColorHex(0xF8F8F8);
  24. [self.view addSubview:self.webView];
  25. WS(weakSelf);
  26. [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
  27. make.top.mas_equalTo(weakSelf.NavBarV.mas_bottom);
  28. make.left.right.mas_equalTo(weakSelf.view);
  29. if (@available(iOS 11.0, *)) {
  30. make.bottom.equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom);
  31. } else {
  32. make.bottom.equalTo(weakSelf.view.mas_bottom);
  33. }
  34. }];
  35. self.webView.UIDelegate = self;
  36. // 导航代理
  37. self.webView.navigationDelegate = self;
  38. self.webView.scrollView.delegate = self;
  39. // self.automaticallyAdjustsScrollViewInsets = YES;
  40. // NSURL *pathUrl = [NSURL URLWithString:@"https://apk.tederen.com/mobile/appeditor"];
  41. NSURL *pathUrl = [NSURL URLWithString:HtmlHost(@"/mobile/appeditor")];
  42. NSURLRequest *request = [NSURLRequest requestWithURL:pathUrl];
  43. [self.webView loadRequest:request];
  44. [self.DoneBtn setAction:^{
  45. NSString *jsonString = [NSString stringWithFormat:@"getEditorText()"];
  46. [weakSelf.webView evaluateJavaScript:jsonString completionHandler:^(id _Nullable data, NSError * _Nullable error) {
  47. NSLog(@"submit:%@",data);
  48. if ([data isKindOfClass:[NSDictionary class]]) {
  49. NSString * html = data[@"html"];
  50. if ([data[@"html"] isKindOfClass:[NSNull class]]) {
  51. html = @"";
  52. }
  53. NSDictionary * dict = @{@"Id":@(weakSelf.Id),
  54. @"Introduce":html
  55. };
  56. SHOWLOADING
  57. [[HttpManager sharedHttpManager] POSTUrl:Host(APP_Group_Update) parameters:dict responseStyle:DATA success:^(id _Nonnull responseObject) {
  58. REMOVESHOW
  59. SHOWSUCCESS(@"编辑成功");
  60. if (weakSelf.UpDateBlock) {
  61. weakSelf.UpDateBlock();
  62. }
  63. __block BOOL isFound = NO;
  64. dispatch_async(dispatch_get_main_queue(), ^{
  65. for (UIViewController * vc in weakSelf.navigationController.viewControllers) {
  66. if ([vc isKindOfClass:NSClassFromString(@"GroupSynopsisVC")]
  67. ||[vc isKindOfClass:NSClassFromString(@"TDGroupInfoDetailVC")]) {
  68. isFound = YES;
  69. [[NSNotificationCenter defaultCenter] postNotificationName:GROUPINFOUPDATE object:nil];
  70. [weakSelf.navigationController popToViewController:vc animated:YES];
  71. }
  72. }
  73. if (!isFound) {
  74. [weakSelf.navigationController popViewControllerAnimated:YES];
  75. }
  76. });
  77. } failure:^(NSError * _Nonnull error) {
  78. REMOVESHOW
  79. }];
  80. }
  81. }];
  82. }];
  83. }
  84. - (NSString *)arrayToJSONString:(NSArray *)array
  85. {
  86. if (array.count == 0) {
  87. return @"";
  88. }
  89. NSError *error = nil;
  90. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:&error];
  91. NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  92. NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
  93. NSRange range = {0,jsonString.length};
  94. //去掉字符串中的空格
  95. [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
  96. NSRange range2 = {0,mutStr.length};
  97. //去掉字符串中的换行符
  98. [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
  99. return mutStr;
  100. }
  101. #pragma mark - layload
  102. - (WKWebView *)webView
  103. {
  104. if (!_webView) {
  105. WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
  106. // 实例化对象
  107. WKUserContentController* userContent = [[WKUserContentController alloc] init];
  108. [userContent addScriptMessageHandler:self name:@"getType"];
  109. configuration.userContentController = userContent;
  110. WKPreferences *preferences = [WKPreferences new];
  111. preferences.javaScriptCanOpenWindowsAutomatically = YES;
  112. preferences.minimumFontSize = 10.0;
  113. configuration.preferences = preferences;
  114. // 初始化WKWebView
  115. _webView = [[WKWebView alloc]initWithFrame:CGRectZero configuration:configuration];
  116. _NoInputAccessoryView *noInputView = [[_NoInputAccessoryView alloc] init];
  117. [noInputView removeInputAccessoryViewFromWKWebView:_webView];
  118. // UI代理
  119. }
  120. return _webView;
  121. }
  122. #pragma mark - WKScriptMessageHandler
  123. - (void)userContentController:(nonnull WKUserContentController *)userContentController didReceiveScriptMessage:(nonnull WKScriptMessage *)message {
  124. NSLog(@"messageName:%@",message.name);
  125. if ([message.name isEqualToString:@"getType"]) {
  126. NSString *jsonString = [NSString stringWithFormat:@"getType('%d','%d','%ld','%@','%d')",99,1,(long)self.Id,USERDEFAULTSGET(@"LOGINTOKEN"),0];
  127. [self.webView evaluateJavaScript:jsonString completionHandler:^(id _Nullable data, NSError * _Nullable error) {
  128. }];
  129. }
  130. }
  131. - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(nonnull NSString *)message initiatedByFrame:(nonnull WKFrameInfo *)frame completionHandler:(nonnull void (^)(void))completionHandler
  132. {
  133. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message?:@"" preferredStyle:UIAlertControllerStyleAlert];
  134. [alertController addAction:([UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  135. }])];
  136. [self presentViewController:alertController animated:YES completion:nil];
  137. NSLog(@"Aleart=========%@",message);
  138. completionHandler();
  139. }
  140. - (void)dealloc {
  141. [self.webView stopLoading];
  142. self.webView.UIDelegate = nil;
  143. self.webView.navigationDelegate = nil;
  144. self.webView.scrollView.delegate = nil;
  145. [self.webView removeFromSuperview];
  146. WKWebsiteDataStore *dateStore = [WKWebsiteDataStore defaultDataStore];
  147. [dateStore fetchDataRecordsOfTypes:[WKWebsiteDataStore allWebsiteDataTypes]
  148. completionHandler:^(NSArray * __nonnull records) {
  149. for (WKWebsiteDataRecord *record in records)
  150. {
  151. [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:record.dataTypes
  152. forDataRecords:@[record]
  153. completionHandler:^{
  154. NSLog(@"Cookies for %@ deleted successfully",record.displayName);
  155. }];
  156. }
  157. }];
  158. WKUserContentController *userCC = self.webView.configuration.userContentController;
  159. [userCC removeScriptMessageHandlerForName:@"getType"];
  160. [userCC removeAllUserScripts];
  161. [[NSNotificationCenter defaultCenter] removeObserver:self];
  162. }
  163. @end