// // WebDetailCell.m // smartRhino // // Created by niuzhen on 2020/2/24. // Copyright © 2020 tederen. All rights reserved. // #import "WebDetailCell.h" #import "NoticeUnreadVC.h" @interface WebDetailCell() @property (nonatomic, assign) CGFloat H; @end @implementation WebDetailCell - (void)awakeFromNib { [super awakeFromNib]; // Initialization code } - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if(self){ [self.contentView addSubview:self.webView]; [self.webView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_offset(UIEdgeInsetsZero); }]; self.webView.UIDelegate = self; // 导航代理 self.webView.navigationDelegate = self; self.webView.scrollView.delegate = self; } return self; } - (WKWebView *)webView { if (!_webView) { WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init]; // 实例化对象 WKUserContentController* userContent = [[WKUserContentController alloc] init]; [userContent addScriptMessageHandler:self name:@"delelteAction"]; [userContent addScriptMessageHandler:self name:@"editAction"]; [userContent addScriptMessageHandler:self name:@"postTopicDetailsData"]; [userContent addScriptMessageHandler:self name:@"alreadyRead"]; [userContent addScriptMessageHandler:self name:@"postHeight"]; [userContent addScriptMessageHandler:self name:@"to"]; configuration.userContentController = userContent; WKPreferences *preferences = [WKPreferences new]; preferences.javaScriptCanOpenWindowsAutomatically = YES; preferences.minimumFontSize = 40.0; configuration.preferences = preferences; // 初始化WKWebView _webView = [[WKWebView alloc]initWithFrame:CGRectZero configuration:configuration]; _webView.scrollView.scrollEnabled = NO; _webView.scrollView.bounces = NO; _webView.scrollView.showsVerticalScrollIndicator = NO; _webView.autoresizingMask = UIViewAutoresizingFlexibleHeight; } return _webView; } - (void)loadData { NSString * url = nil; switch (self.type) { case CreateGroupType: { url = @"/#/mobile/topicDetails"; } break; case CreateTopicType: { url = @"/#/mobile/topicDetails"; } break; case CreateNotesType: { url = @"/#/mobile/topicDetails"; } break; case CreateNoticeType: { url = @"/#/mobile/noticeDetails"; } break; case CreateCommonType: { url = @"/#/mobile/noticeDetails"; } break; case CreateAnonymousType: { url = @"/#/mobile/noticeDetails"; } break; default: { url = @"/#/mobile/topicDetails"; } break; } NSURL *pathUrl = [NSURL URLWithString:HtmlHost(url)]; // NSURL *pathUrl = [NSURL URLWithString:@"http://192.168.0.119:8080/#/mobile/topicDetails"]; NSURLRequest *request = [NSURLRequest requestWithURL:pathUrl]; self.H = 0.1f; [self.webView loadRequest:request]; } #pragma mark - WKScriptMessageHandler - (void)userContentController:(nonnull WKUserContentController *)userContentController didReceiveScriptMessage:(nonnull WKScriptMessage *)message { if ([message.name isEqualToString:@"delelteAction"]) { [self deleteButtonHander:self.Id]; } if ([message.name isEqualToString:@"editAction"]) { [self editorButtonHander:self.Id]; } if ([message.name isEqualToString:@"postTopicDetailsData"]) { NSString *jsonString = [NSString stringWithFormat:@"getData('%ld','%@','%d')",(long)self.Id,USERDEFAULTSGET(@"LOGINTOKEN"),self.type]; [self.webView evaluateJavaScript:jsonString completionHandler:^(id _Nullable data, NSError * _Nullable error) { NSLog(@"data:%@",data); NSLog(@"error:%@",error); }]; } if ([message.name isEqualToString:@"alreadyRead"]) { NSLog(@"msgBody:%@",message.body); NSDictionary * dict = (NSDictionary *)message.body; if (self.clickReadBlock) { self.clickReadBlock(dict); } // NoticeUnreadVC *vc = [NoticeUnreadVC initNoticeUnreadVC]; // vc.Id = 622; // vc.type = ReadNoticeType; // vc.readCount = [dict[@"ReadCount"] integerValue]; // vc.unReadCount = [dict[@"AllCount"] integerValue] - [dict[@"ReadCount"] integerValue]; } if ([message.name isEqualToString:@"to"]) { NSLog(@"msgBody:%@",message.body); NSDictionary * dict = (NSDictionary *)message.body; if (self.clickDetailBlock) { self.clickDetailBlock(dict); } } if ([message.name isEqualToString:@"postHeight"]) { CGFloat H = [message.body floatValue]; if (H != 0 && H != self.H) { self.H = H; if (self.upDateCellheightBlock) { self.upDateCellheightBlock(H); } } } } #pragma mark - WKScriptMessageHandler -(NSString *)convertToJson:(NSDictionary *)dict { NSError *error; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error]; NSString *jsonString; if (!jsonData) { NSLog(@"%@",error); }else{ 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 - WKUIDelegete - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(nonnull NSString *)message initiatedByFrame:(nonnull WKFrameInfo *)frame completionHandler:(nonnull void (^)(void))completionHandler { completionHandler(); } - (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(nonnull NSString *)message initiatedByFrame:(nonnull WKFrameInfo *)frame completionHandler:(nonnull void (^)(BOOL))completionHandler { completionHandler(YES); } - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(nonnull NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(nonnull WKFrameInfo *)frame completionHandler:(nonnull void (^)(NSString * _Nullable))completionHandler { completionHandler(@"111"); } #pragma mark - 撤回 - (void)deleteButtonHander:(NSInteger)deleteId{ if (self.clickDeleteBlock) { self.clickDeleteBlock(); } } #pragma mark - 编辑 - (void)editorButtonHander:(NSInteger)editId{ if (self.clickEditBlock) { self.clickEditBlock(); } } - (NSString *)arrayToJSONString:(NSArray *)array { NSError *error = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:&error]; NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; return jsonString; } - (void)dealloc { 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:@"delelteAction"]; [userCC removeScriptMessageHandlerForName:@"delelteAction"]; [userCC removeScriptMessageHandlerForName:@"postTopicDetailsData"]; [userCC removeScriptMessageHandlerForName:@"alreadyRead"]; [userCC removeScriptMessageHandlerForName:@"postHeight"]; [userCC removeScriptMessageHandlerForName:@"to"]; [[NSNotificationCenter defaultCenter] removeObserver:self]; [self.webView stopLoading]; self.webView.UIDelegate = nil; self.webView.navigationDelegate = nil; self.webView.scrollView.delegate = nil; [self.webView removeFromSuperview]; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end