WebDetailCell.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // WebDetailCell.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2020/2/24.
  6. // Copyright © 2020 tederen. All rights reserved.
  7. //
  8. #import "WebDetailCell.h"
  9. #import "NoticeUnreadVC.h"
  10. @interface WebDetailCell()<WKUIDelegate,WKNavigationDelegate,UIScrollViewDelegate,WKScriptMessageHandler>
  11. @property (nonatomic, assign) CGFloat H;
  12. @end
  13. @implementation WebDetailCell
  14. - (void)awakeFromNib {
  15. [super awakeFromNib];
  16. // Initialization code
  17. }
  18. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  19. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  20. if(self){
  21. [self.contentView addSubview:self.webView];
  22. [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
  23. make.edges.mas_offset(UIEdgeInsetsZero);
  24. }];
  25. self.webView.UIDelegate = self;
  26. // 导航代理
  27. self.webView.navigationDelegate = self;
  28. self.webView.scrollView.delegate = self;
  29. }
  30. return self;
  31. }
  32. - (WKWebView *)webView
  33. {
  34. if (!_webView) {
  35. WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
  36. // 实例化对象
  37. WKUserContentController* userContent = [[WKUserContentController alloc] init];
  38. [userContent addScriptMessageHandler:self name:@"delelteAction"];
  39. [userContent addScriptMessageHandler:self name:@"editAction"];
  40. [userContent addScriptMessageHandler:self name:@"postTopicDetailsData"];
  41. [userContent addScriptMessageHandler:self name:@"alreadyRead"];
  42. [userContent addScriptMessageHandler:self name:@"postHeight"];
  43. [userContent addScriptMessageHandler:self name:@"to"];
  44. configuration.userContentController = userContent;
  45. WKPreferences *preferences = [WKPreferences new];
  46. preferences.javaScriptCanOpenWindowsAutomatically = YES;
  47. preferences.minimumFontSize = 40.0;
  48. configuration.preferences = preferences;
  49. // 初始化WKWebView
  50. _webView = [[WKWebView alloc]initWithFrame:CGRectZero configuration:configuration];
  51. _webView.scrollView.scrollEnabled = NO;
  52. _webView.scrollView.bounces = NO;
  53. _webView.scrollView.showsVerticalScrollIndicator = NO;
  54. _webView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
  55. }
  56. return _webView;
  57. }
  58. - (void)loadData
  59. {
  60. NSString * url = nil;
  61. switch (self.type) {
  62. case CreateGroupType:
  63. {
  64. url = @"/#/mobile/topicDetails";
  65. }
  66. break;
  67. case CreateTopicType:
  68. {
  69. url = @"/#/mobile/topicDetails";
  70. }
  71. break;
  72. case CreateNotesType:
  73. {
  74. url = @"/#/mobile/topicDetails";
  75. }
  76. break;
  77. case CreateNoticeType:
  78. {
  79. url = @"/#/mobile/noticeDetails";
  80. }
  81. break;
  82. case CreateCommonType:
  83. {
  84. url = @"/#/mobile/noticeDetails";
  85. }
  86. break;
  87. case CreateAnonymousType:
  88. {
  89. url = @"/#/mobile/noticeDetails";
  90. }
  91. break;
  92. default:
  93. {
  94. url = @"/#/mobile/topicDetails";
  95. }
  96. break;
  97. }
  98. NSURL *pathUrl = [NSURL URLWithString:HtmlHost(url)];
  99. // NSURL *pathUrl = [NSURL URLWithString:@"http://192.168.0.119:8080/#/mobile/topicDetails"];
  100. NSURLRequest *request = [NSURLRequest requestWithURL:pathUrl];
  101. self.H = 0.1f;
  102. [self.webView loadRequest:request];
  103. }
  104. #pragma mark - WKScriptMessageHandler
  105. - (void)userContentController:(nonnull WKUserContentController *)userContentController didReceiveScriptMessage:(nonnull WKScriptMessage *)message {
  106. if ([message.name isEqualToString:@"delelteAction"]) {
  107. [self deleteButtonHander:self.Id];
  108. }
  109. if ([message.name isEqualToString:@"editAction"]) {
  110. [self editorButtonHander:self.Id];
  111. }
  112. if ([message.name isEqualToString:@"postTopicDetailsData"]) {
  113. NSString *jsonString = [NSString stringWithFormat:@"getData('%ld','%@','%d')",(long)self.Id,USERDEFAULTSGET(@"LOGINTOKEN"),self.type];
  114. [self.webView evaluateJavaScript:jsonString completionHandler:^(id _Nullable data, NSError * _Nullable error) {
  115. NSLog(@"data:%@",data);
  116. NSLog(@"error:%@",error);
  117. }];
  118. }
  119. if ([message.name isEqualToString:@"alreadyRead"]) {
  120. NSLog(@"msgBody:%@",message.body);
  121. NSDictionary * dict = (NSDictionary *)message.body;
  122. if (self.clickReadBlock) {
  123. self.clickReadBlock(dict);
  124. }
  125. // NoticeUnreadVC *vc = [NoticeUnreadVC initNoticeUnreadVC];
  126. // vc.Id = 622;
  127. // vc.type = ReadNoticeType;
  128. // vc.readCount = [dict[@"ReadCount"] integerValue];
  129. // vc.unReadCount = [dict[@"AllCount"] integerValue] - [dict[@"ReadCount"] integerValue];
  130. }
  131. if ([message.name isEqualToString:@"to"]) {
  132. NSLog(@"msgBody:%@",message.body);
  133. NSDictionary * dict = (NSDictionary *)message.body;
  134. if (self.clickDetailBlock) {
  135. self.clickDetailBlock(dict);
  136. } }
  137. if ([message.name isEqualToString:@"postHeight"]) {
  138. CGFloat H = [message.body floatValue];
  139. if (H != 0 && H != self.H) {
  140. self.H = H;
  141. if (self.upDateCellheightBlock) {
  142. self.upDateCellheightBlock(H);
  143. }
  144. }
  145. }
  146. }
  147. #pragma mark - WKScriptMessageHandler
  148. -(NSString *)convertToJson:(NSDictionary *)dict
  149. {
  150. NSError *error;
  151. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
  152. NSString *jsonString;
  153. if (!jsonData) {
  154. NSLog(@"%@",error);
  155. }else{
  156. jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
  157. }
  158. NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
  159. NSRange range = {0,jsonString.length};
  160. //去掉字符串中的空格
  161. [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
  162. NSRange range2 = {0,mutStr.length};
  163. //去掉字符串中的换行符
  164. [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
  165. return mutStr;
  166. }
  167. #pragma mark - WKUIDelegete
  168. - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(nonnull NSString *)message initiatedByFrame:(nonnull WKFrameInfo *)frame completionHandler:(nonnull void (^)(void))completionHandler
  169. {
  170. completionHandler();
  171. }
  172. - (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(nonnull NSString *)message initiatedByFrame:(nonnull WKFrameInfo *)frame completionHandler:(nonnull void (^)(BOOL))completionHandler
  173. {
  174. completionHandler(YES);
  175. }
  176. - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(nonnull NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(nonnull WKFrameInfo *)frame completionHandler:(nonnull void (^)(NSString * _Nullable))completionHandler
  177. {
  178. completionHandler(@"111");
  179. }
  180. #pragma mark - 撤回
  181. - (void)deleteButtonHander:(NSInteger)deleteId{
  182. if (self.clickDeleteBlock) {
  183. self.clickDeleteBlock();
  184. }
  185. }
  186. #pragma mark - 编辑
  187. - (void)editorButtonHander:(NSInteger)editId{
  188. if (self.clickEditBlock) {
  189. self.clickEditBlock();
  190. }
  191. }
  192. - (NSString *)arrayToJSONString:(NSArray *)array
  193. {
  194. NSError *error = nil;
  195. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:&error];
  196. NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  197. return jsonString;
  198. }
  199. - (void)dealloc {
  200. WKWebsiteDataStore *dateStore = [WKWebsiteDataStore defaultDataStore];
  201. [dateStore fetchDataRecordsOfTypes:[WKWebsiteDataStore allWebsiteDataTypes]
  202. completionHandler:^(NSArray * __nonnull records) {
  203. for (WKWebsiteDataRecord *record in records)
  204. {
  205. [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:record.dataTypes
  206. forDataRecords:@[record]
  207. completionHandler:^{
  208. NSLog(@"Cookies for %@ deleted successfully",record.displayName);
  209. }];
  210. }
  211. }];
  212. WKUserContentController *userCC = self.webView.configuration.userContentController;
  213. [userCC removeScriptMessageHandlerForName:@"delelteAction"];
  214. [userCC removeScriptMessageHandlerForName:@"delelteAction"];
  215. [userCC removeScriptMessageHandlerForName:@"postTopicDetailsData"];
  216. [userCC removeScriptMessageHandlerForName:@"alreadyRead"];
  217. [userCC removeScriptMessageHandlerForName:@"postHeight"];
  218. [userCC removeScriptMessageHandlerForName:@"to"];
  219. [[NSNotificationCenter defaultCenter] removeObserver:self];
  220. [self.webView stopLoading];
  221. self.webView.UIDelegate = nil;
  222. self.webView.navigationDelegate = nil;
  223. self.webView.scrollView.delegate = nil;
  224. [self.webView removeFromSuperview];
  225. }
  226. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  227. [super setSelected:selected animated:animated];
  228. // Configure the view for the selected state
  229. }
  230. @end