123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- //
- // WebDetailCell.m
- // smartRhino
- //
- // Created by niuzhen on 2020/2/24.
- // Copyright © 2020 tederen. All rights reserved.
- //
- #import "WebDetailCell.h"
- #import "NoticeUnreadVC.h"
- @interface WebDetailCell()<WKUIDelegate,WKNavigationDelegate,UIScrollViewDelegate,WKScriptMessageHandler>
- @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
|