OldArticleDetailViewController.m
27.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//
// OldArticleDetailViewController.m
// CNLiveScioLive
//
// Created by CNLive-zxw on 2018/9/27.
// Copyright © 2018年 CNLive. All rights reserved.
//
#import "OldArticleDetailViewController.h"
#import "OldVideoDetailViewController.h"
#import "VideoTableViewCell.h"
#import "ArticleTableViewCell.h"
#import "YWWebScrollView.h"
#import "Html5Utils.h"
@interface OldArticleDetailViewController ()<UIGestureRecognizerDelegate>{
NSMutableArray *_imageArray;
NSInteger typeFlag;
NSString *htmlString;
NSString *htmlUrl;
BOOL isHaveVideo;
}
@end
@implementation OldArticleDetailViewController
- (id)initWithDictionary:(NSDictionary*)dictionary{
if (self = [super init]) {
_dictionary = [NSDictionary dictionaryWithDictionary:dictionary];
_imageArray = [[ NSMutableArray alloc] init];
NSString *url = [NSString stringWithFormat:@"%@?from=ios",dictionary[@"pageUrl"]];
htmlString = [NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:nil];
//替换掉html页面中的展示js
NSString *replace=[htmlString stringByReplacingOccurrencesOfString:@"previewImage.start(obj);" withString:@""];
htmlString=replace;
htmlUrl=dictionary[@"pageUrl"];
if ([htmlString containsString:@"<video"]) {
isHaveVideo = YES;
[[JGMusicManager shareMusicManager] playStop];
}
}
return self;
}
- (NSString *)setHTMLString {
NSString* htmlHeader = [[NSString alloc] initWithFormat:@"<p><font style=\"font-size:20px;\"><strong>%@</strong></font><p>%@</p><font color=\"#184085\">%@</font> </p>",[_dictionary objectForKey:@"title"],[_dictionary objectForKey:@"createDate"],@"来源 : 国新网"];
NSString * htmlcontent = [NSString stringWithFormat:@"<div id=\"webview_content_wrapper\">%@<font style=\"font-size:%@px;\">%@</font></div>",htmlHeader,fontArray[fontIndex],[_dicArticle objectForKey:@"content"]];
NSLog(@"htmlcontent = %@",htmlcontent);
return htmlcontent;
}
- (void)loadData {
// [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_dictionary[@"pageUrl"]]]];
// htmlString = [htmlString stringByReplacingOccurrencesOfString:@"<img" withString:@"<img style=\"width:100%; height:auto;\""];
[_webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:htmlUrl]];
}
//加载相关文章
- (void)loadProgramRelativeData {
if (![[Reachability reachabilityForInternetConnection] currentReachabilityStatus]) {
[AlertViewTool showHUDViewText:CustomStr(@"NetworkiInterruption")];
return;
}
NSString *strUrl = @"http://app.phonemovie.cnlive.com/gxb/api/programRelative";
NSString *categoryId = [NSString stringWithFormat:@"{\"plat\":\"i\",\"version\":\"5.0\",\"appid\":\"gxb\",\"cmsContentId\":\"%@\"}",_dictionary[@"cmsContentId"]];
NSDictionary *parameters = @{@"params":categoryId};
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:strUrl
parameters:parameters headers:nil
progress:^(NSProgress * _Nonnull uploadProgress) {}
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 请求成功,解析数据
NSDictionary *_dic = [NSDictionary dictionaryWithDictionary:responseObject];
NSLog(@"%@",_dic);
if ([_dic[@"errorCode"] isEqualToString:@"0"]) {
self->hasLoad = YES;
NSArray *array = _dic[@"programs"];
if (0 != [array count]) {
self->_newsArray = [NSArray arrayWithArray:array];
[self.tableView setFrame:CGRectMake(0, [self->heightArr[self->fontIndex] floatValue], MainScreenWidth, 36 + 86 * self->_newsArray.count)];
[self.scrollView setContentSize:CGSizeMake(MainScreenWidth, [self->heightArr[self->fontIndex] floatValue] + self.tableView.frame.size.height)];
[self.tableView reloadData];
}
} else {
[AlertViewTool showHUDViewText:_dic[@"errorMessage"]];
}
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// 请求失败
NSLog(@"%@", [error localizedDescription]);
[AlertViewTool showHUDViewText:CustomStr(@"ServerBusy")];
}];
}
- (void)loadMoreData {
[self.navigationController popToRootViewControllerAnimated:YES];
}
//- (void)setNaviation{
// self.navigationController.navigationBar.translucent = NO;
// [self.navigationController.navigationBar setBarTintColor:HEXCOLOR(0x195ac9)];
// [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:20],NSFontAttributeName,[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
// UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
// [leftButton addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
// [leftButton setImage:[UIImage imageNamed:@"cnlive_white_back"] forState:UIControlStateNormal];
// [leftButton setFrame:CGRectMake(0, 0, 44, 44)];
// self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
// self.navigationItem.hidesBackButton = YES;
// self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
//}
#pragma mark - UI
- (void)setUpNavigationBar{
self.navigationController.navigationBar.barTintColor = Color_Blue;
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont systemFontOfSize:18]};
//左
UIButton *headerButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 44)];
headerButton.layer.cornerRadius = 2.0;
headerButton.layer.masksToBounds = true;
[headerButton setImage:[UIImage imageNamed:@"cnlive_white_back"] forState:UIControlStateNormal];
[headerButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:headerButton];
self.navigationItem.title = _dictionary[@"cmsColumnName"];
}
- (void)viewDidDisappear:(BOOL)animated{
[(AppDelegate *)[UIApplication sharedApplication].delegate initializeSDKConfiguration];
}
- (void)goBack{
[(AppDelegate *)[UIApplication sharedApplication].delegate initializeSDKConfiguration];
[self.navigationController popViewControllerAnimated:YES];
}
- (void)loadShareView {
UIImageView *lineImage = [[UIImageView alloc] init];
if (iPhoneX) {
lineImage.frame = CGRectMake(0, MainScreenHeight - 44 - 34 - 88, MainScreenWidth, 1);
} else {
lineImage.frame = CGRectMake(0, MainScreenHeight - 64 - 44, MainScreenWidth, 1);
}
lineImage.backgroundColor = Color_Line;
[self.view addSubview:lineImage];
UIButton *fontSizeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[fontSizeButton setImage:[UIImage imageNamed:@"cnlive_fontsize"] forState:UIControlStateNormal];
if (iPhoneX) {
[fontSizeButton setFrame:CGRectMake((MainScreenWidth/2 - 33)/2, MainScreenHeight - 44 + (44 - 22)/2 - 34 - 88, 33, 22)];
} else {
[fontSizeButton setFrame:CGRectMake((MainScreenWidth/2 - 33)/2, MainScreenHeight - 44 - 64 + (44 - 22)/2, 33, 22)];
}
[fontSizeButton addTarget:self action:@selector(shareButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:fontSizeButton];
[fontSizeButton setTag:10000];
UIButton *isFavoriteButton = [UIButton buttonWithType:UIButtonTypeCustom];
TableData *notice = [[TableData alloc] initWithJSONDictionary:_dictionary];
BOOL isIn = [[DBAction sharedDBAction] isInTable:notice];
if (isIn) {
[isFavoriteButton setImage:[UIImage imageNamed:@"cnlive_isfavorited"] forState:UIControlStateNormal];
} else {
[isFavoriteButton setImage:[UIImage imageNamed:@"cnlive_isfavorite"] forState:UIControlStateNormal];
}
if (iPhoneX) {
[isFavoriteButton setFrame:CGRectMake((MainScreenWidth - 23)/2, MainScreenHeight - 44 + (44 - 22)/2 - 34 - 88, 33, 22)];
} else {
[isFavoriteButton setFrame:CGRectMake((MainScreenWidth - 23)/2, MainScreenHeight - 44 - 64 + (44 - 22)/2, 33, 22)];
}
[isFavoriteButton addTarget:self action:@selector(shareButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:isFavoriteButton];
[isFavoriteButton setTag:20000];
UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
[shareButton setImage:[UIImage imageNamed:@"cnlive_share"] forState:UIControlStateNormal];
if (iPhoneX) {
[shareButton setFrame:CGRectMake((MainScreenWidth*3/2 - 22)/2, MainScreenHeight - 44 + (44 - 22)/2 - 34 - 88, 33, 22)];
} else {
[shareButton setFrame:CGRectMake((MainScreenWidth*3/2 - 22)/2, MainScreenHeight - 44 - 64 + (44 - 22)/2, 33, 22)];
}
[shareButton addTarget:self action:@selector(shareButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:shareButton];
[shareButton setTag:30000];
}
- (void)shareButtonTaped:(id)sender {
UIButton *btn = (UIButton *)sender;
if (10000 == btn.tag) {
if ([fontArray count] - 1 == fontIndex) {
fontIndex = 0;
} else {
fontIndex++;
}
if (0 == fontIndex) {
NSLog(@"0");
[_webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'" completionHandler:nil];
} else if (1 == fontIndex) {
NSLog(@"1");
[_webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '120%'" completionHandler:nil];
} else if (2 == fontIndex) {
NSLog(@"2");
[_webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '140%'" completionHandler:nil];
}
} else if (20000 == btn.tag) {
TableData *notice = [[TableData alloc] initWithJSONDictionary:_dictionary];
BOOL isIn = [[DBAction sharedDBAction] isInTable:notice];
if (!isIn) {
BOOL isInsert = [[DBAction sharedDBAction] insertTable:notice];
if (isInsert) {
[AlertViewTool showHUDAddedToView:self.view text:CustomStr(@"CollectSuccess")];
[btn setImage:[UIImage imageNamed:@"cnlive_isfavorited"] forState:UIControlStateNormal];
}
} else {
BOOL isDelete = [[DBAction sharedDBAction] deleteTable:notice];
if (isDelete) {
[AlertViewTool showHUDAddedToView:self.view text:CustomStr(@"CancelCollect")];
[btn setImage:[UIImage imageNamed:@"cnlive_isfavorite"] forState:UIControlStateNormal];
}
}
} else if (30000 == btn.tag) {
NSString *img = [NSString stringWithFormat:@"%@",_dictionary[@"img"]];
NSString *title = [NSString stringWithFormat:@"%@",_dictionary[@"title"]];
NSString *text = [NSString stringWithFormat:@"%@",!_dictionary[@"desc"]||[_dictionary[@"desc"] isEqualToString:@""]?title:_dictionary[@"desc"]];
NSString *url = [NSString stringWithFormat:@"%@",_dictionary[@"pageUrl"]];
[[TemplateShareManager manager] shareImage:img title:title text:text url:url controller:self];
return;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// self.automaticallyAdjustsScrollViewInsets = NO;
self.view.backgroundColor = [UIColor whiteColor];
[self setUpNavigationBar];
if (iPhoneX) {
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, MainScreenHeight - 88 - 44 - 34)];
} else {
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, MainScreenHeight - 64 - 44)];
}
self.scrollView.backgroundColor = [UIColor whiteColor];
// MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
// MJRefreshBackGifFooter *footer = [MJRefreshBackGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
// NSMutableArray *refreshingImages = [NSMutableArray array];
// for (NSUInteger i = 1; i <= 4; i++) {
// UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"refresh0%zd", i]];
// [refreshingImages addObject:image];
// }
// [footer setImages:refreshingImages forState:MJRefreshStateRefreshing];
// [footer setTitle:@"上拉关闭当前页" forState:MJRefreshStateIdle];
// [footer setTitle:@"放开关闭当前页" forState:MJRefreshStatePulling];
// [footer setTitle:@"正在关闭当前页" forState:MJRefreshStateRefreshing];
// [self.scrollView setHeader:header];
// [self.scrollView setFooter:footer];
[self.view addSubview:self.scrollView];
// _progressProxy = [[NJKWebViewProgress alloc] init];
// _progressProxy.webViewProxyDelegate = self;
// _progressProxy.progressDelegate = self;
// CGFloat progressBarHeight = 2.f;
// CGRect navigationBarBounds = self.navigationController.navigationBar.bounds;
// CGRect barFrame = CGRectMake(0, navigationBarBounds.size.height, navigationBarBounds.size.width, progressBarHeight);
// _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
// _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *wkUController = [[WKUserContentController alloc] init];
[wkUController addUserScript:wkUScript];
WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
wkWebConfig.userContentController = wkUController;
if (iPhoneX) {
_webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, MainScreenHeight - 88 - 44 - 34) configuration:wkWebConfig];
} else {
_webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, MainScreenHeight - 64 - 44) configuration:wkWebConfig];
}
_webView.scrollView.backgroundColor = [UIColor whiteColor];
// _webView.dataDetectorTypes = UIDataDetectorTypeNone;
_webView.backgroundColor = [UIColor whiteColor];
_webView.navigationDelegate = self;
_webView.UIDelegate = self;
// _webView.delegate = _progressProxy;
// _webView.scalesPageToFit = YES;
_webView.opaque = NO;
[self.scrollView addSubview:_webView];
self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
self.tableView.backgroundColor = [UIColor whiteColor];
self.tableView.backgroundView = nil;
self.tableView.scrollEnabled = NO;
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.scrollView addSubview:self.tableView];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.showsVerticalScrollIndicator = NO;
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
self.errorLabe = [[UILabel alloc] initWithFrame:CGRectZero];
self.errorLabe.backgroundColor = [UIColor clearColor];
self.errorLabe.textColor = HEXCOLOR(0x333333);
self.errorLabe.font = [UIFont systemFontOfSize:15];
[self.scrollView addSubview:self.errorLabe];
self.errorLabe.text = @"没有数据";
self.errorLabe.hidden = YES;
[self loadShareView];
fontIndex = (int)[[NSUserDefaults standardUserDefaults] integerForKey:@"FontSize"];
heightArr = [NSMutableArray arrayWithArray:@[@"0",@"0",@"0"]];
fontArray = @[@"16",@"20",@"24"];
hasLoad = NO;
[self loadData];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: &error];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
//计数统计
if ([_dictionary[@"cmsColumnId"] isEqualToString:@"spzl/srbd/"]) {
//深入报道
[CNLiveStat event:@"srbdsl"];
}else if ([_dictionary[@"cmsColumnId"] isEqualToString:@"spzl/zcjd/"]){
//国新图说
[CNLiveStat event:@"gxtssl"];
}else if ([_dictionary[@"cmsColumnId"] isEqualToString:@"spzl/gzdt/"]){
//要闻
[CNLiveStat event:@"ywsl"];
}else if ([_dictionary[@"cmsColumnId"] isEqualToString:@"spzl/hyjj/"]){
//焦点
[CNLiveStat event:@"jdsl"];
}else if ([_dictionary[@"cmsColumnId"] isEqualToString:@"spzl/lingdaoyaowen/"]){
//领导要闻
[CNLiveStat event:@"ldywsl"];
}else if ([_dictionary[@"cmsColumnId"] isEqualToString:@"spzl/bw/"]){
//部委
[CNLiveStat event:@"bwsl"];
}else if ([_dictionary[@"cmsColumnId"] isEqualToString:@"spzl/dfdt/"]){
//地方外宣
[CNLiveStat event:@"dfwxsl"];
}else if ([_dictionary[@"cmsColumnId"] isEqualToString:@"spzl/scio/"]){
//scio news
[CNLiveStat event:@"newssl"];
}else if ([_dictionary[@"cmsColumnId"] isEqualToString:@"spzl/wqtw/"]){
//往期图文
[CNLiveStat event:@"wqtwsl"];
}else if ([_dictionary[@"cmsColumnId"] isEqualToString:@"spzl/fbhgg/"]){
//发布会公告
[CNLiveStat event:@"fbhggsl"];
}else if ([_dictionary[@"cmsColumnId"] isEqualToString:@"spzl/toutiao/"]){
//头条
[CNLiveStat event:@"ttsl"];
}else if ([_dictionary[@"cmsColumnId"] isEqualToString:@"spzl/yidaiyilu/"]){
//一带一路
[CNLiveStat event:@"ydylsl"];
}else if ([_dictionary[@"cmsColumnId"] isEqualToString:@"spzl/zixun/"]){
//资讯
[CNLiveStat event:@"zxsl"];
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
#pragma mark - WebView加载H5页面区域
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
if (_imageArray.count) {
NSURL *requestURL = webView.URL;
BOOL isImg = [@"img" isEqualToString:[requestURL scheme]];
BOOL isHttp = [[requestURL scheme] isEqualToString: @"http"];
BOOL isHttps = [[requestURL scheme] isEqualToString: @"https"];
BOOL isMailto = [[requestURL scheme] isEqualToString: @"mailto"];
if (isImg) {
NSString *requesturl = [[NSString stringWithFormat:@"%@",webView.URL] substringFromIndex:4];
for (NSInteger indes = 0; indes < _imageArray.count; indes++) {
NSString *url = [NSString stringWithFormat:@"%@",_imageArray[indes]];
if ([url isEqualToString:requesturl]) {
[self pushPicController:requesturl withPoint:indes];//显示图集
break;
}
}
} else if (isHttp || isHttps || isMailto) {
[[UIApplication sharedApplication] openURL:webView.URL];
decisionHandler(WKNavigationActionPolicyCancel);
}
} else {
NSURL *requestURL = webView.URL;
if (([[requestURL scheme] isEqualToString: @"http"] || [[requestURL scheme] isEqualToString:@"https"] || [[requestURL scheme] isEqualToString: @"mailto" ]) && (navigationAction.navigationType == WKNavigationTypeLinkActivated)) {
[[UIApplication sharedApplication] openURL:webView.URL];
decisionHandler(WKNavigationActionPolicyCancel);
}
}
decisionHandler(WKNavigationActionPolicyAllow);
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation*)navigation {
_imageArray = [_webView getImgs];
NSMutableArray *array = [NSMutableArray array];
for (NSString *string in _imageArray) {
if ([string rangeOfString:@"cnliveimg.com/sites/"].location == NSNotFound && [string rangeOfString:@"html"].location == NSNotFound && [string rangeOfString:@"cnliveimg.com/main/"].location == NSNotFound) {
[array addObject:string];
}
}
_imageArray = array;
[webView addClickEventOnImg];//有的图片没有点击事件
//使页面中图片width=100%
[self.webView evaluateJavaScript:
@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"var imgs = document.getElementById('article_content').getElementsByTagName('img')\";"
"for(var i=0; i<imgs.length; i++){"
"imgs[i].style.width = '100%';"
"}"
"document.getElementsByTagName('body')[0].appendChild(script);" completionHandler:nil];
//通过css隐藏h5页面中头部和底部的logo
NSString *cssString = @".container { display:none !important; }";
NSString *javascriptString = @"var style = document.createElement('style'); style.innerHTML = '%@'; document.head.appendChild(style)";
NSString *javascriptWithCSSString = [NSString stringWithFormat:javascriptString, cssString];
[webView evaluateJavaScript:javascriptWithCSSString completionHandler:nil];
CGRect frame = _webView.frame;
frame.size.width = MainScreenWidth;
frame.size.height = 1;
_webView.frame = frame;
CGFloat graphicLiveHeight = _webView.scrollView.contentSize.height;
frame.size.height = _webView.scrollView.contentSize.height;
_webView.frame = frame;
if (0 == [heightArr[fontIndex] floatValue]) {
[heightArr replaceObjectAtIndex:fontIndex withObject:[NSString stringWithFormat:@"%f",graphicLiveHeight]];
}
[self.scrollView.mj_header endRefreshing];
[_webView setFrame:CGRectMake(0, 0, MainScreenWidth, [heightArr[fontIndex] floatValue])];
[self.scrollView setContentSize:CGSizeMake(MainScreenWidth, [heightArr[fontIndex] floatValue])];
if (!hasLoad) {
[self loadProgramRelativeData];
}
}
-(void)pushPicController:(NSString *)url withPoint:(NSInteger)CurrentIndex {
YWWebScrollView *scrollView = [[YWWebScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
scrollView.ScrollViewTag = CurrentIndex;
scrollView.imageUrls = _imageArray;
[[UIApplication sharedApplication].keyWindow addSubview:scrollView];
}
#pragma mark - TableView加载相关文章区域
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_newsArray count];
}
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
return 86.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 30.0;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, 30)];
view.backgroundColor = [UIColor whiteColor];
UIImageView *lineImage1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, 1)];
lineImage1.backgroundColor = Color_Line;
[view addSubview:lineImage1];
UILabel *relatedInformation = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, MainScreenWidth - 24, 30)];
relatedInformation.backgroundColor = [UIColor clearColor];
relatedInformation.textColor = HEXCOLOR(0x333333);
relatedInformation.font = [UIFont systemFontOfSize:15];
[view addSubview:relatedInformation];
relatedInformation.text = CustomStr(@"RelevantInformation");
return view;
}
- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section {
return 6.0;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, 6)];
view.backgroundColor = [UIColor whiteColor];
return view;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *_dic = _newsArray[indexPath.row];
if ([@"program" isEqualToString:_dic[@"type"]]) {
static NSString *identifier = @"videoCell";
VideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[VideoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
[cell.backImage sd_setImageWithURL:[NSURL URLWithString:_dic[@"img"]] placeholderImage:[UIImage imageNamed:@"backDefault"]];
cell.playImage.image = [UIImage imageNamed:@"playButton"];
cell.titleLabel.text = _dic[@"title"];
cell.timeLabel.text = _dic[@"createDate"];
return cell;
} else if ([@"article" isEqualToString:_dic[@"type"]]) {
static NSString *identifier = @"articleCell";
ArticleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[ArticleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
[cell.backImage sd_setImageWithURL:[NSURL URLWithString:_dic[@"img"]] placeholderImage:[UIImage imageNamed:@"backDefault"]];
cell.titleLabel.text = _dic[@"title"];
cell.timeLabel.text = _dic[@"createDate"];
return cell;
}
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%@",_newsArray[indexPath.row]);
NSDictionary *_dic = _newsArray[indexPath.row];
[self pushViewTo:_dic];
}
#pragma mark -
- (void)pushViewTo:(NSDictionary *)_dic {
if ([@"program" isEqualToString:_dic[@"type"]]) {
OldVideoDetailViewController *vc = [[OldVideoDetailViewController alloc] initWithDictionary:_dic];
vc.title = _dic[@"cmsColumnName"];
vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:vc animated:YES];
} else if ([@"article" isEqualToString:_dic[@"type"]]) {
OldArticleDetailViewController *vc = [[OldArticleDetailViewController alloc] initWithDictionary:_dic];
vc.title = _dic[@"cmsColumnName"];
vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:vc animated:YES];
}
}
-(void)viewDidLayoutSubviews{
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)])
[self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])
[self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([cell respondsToSelector:@selector(setSeparatorInset:)])
[cell setSeparatorInset:UIEdgeInsetsZero];
if ([cell respondsToSelector:@selector(setLayoutMargins:)])
[cell setLayoutMargins:UIEdgeInsetsZero];
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
}
@end