Commit f492285842e3f02e5f463292dcf92596aa623ee8
1 parent
5a5164da
no message
Showing
10 changed files
with
396 additions
and
131 deletions
CNLiveScioLive.xcodeproj/project.pbxproj
| ... | ... | @@ -700,6 +700,7 @@ |
| 700 | 700 | 08C090BF215116CB00E23D16 /* CNLiveIndicatorTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 08C090BE215116CB00E23D16 /* CNLiveIndicatorTableViewCell.m */; }; |
| 701 | 701 | 08C090C0215116CB00E23D16 /* CNLiveIndicatorTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 08C090BE215116CB00E23D16 /* CNLiveIndicatorTableViewCell.m */; }; |
| 702 | 702 | 08C090C1215116CB00E23D16 /* CNLiveIndicatorTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 08C090BE215116CB00E23D16 /* CNLiveIndicatorTableViewCell.m */; }; |
| 703 | + 08E7998F24B2F52C00F15587 /* WKWebView+JavaScript.m in Sources */ = {isa = PBXBuildFile; fileRef = 08E7998E24B2F52C00F15587 /* WKWebView+JavaScript.m */; }; | |
| 703 | 704 | 08FB8769215DD1C200DA5423 /* VoiceViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 08FB8767215DD1C200DA5423 /* VoiceViewController.m */; }; |
| 704 | 705 | 08FB876A215DD1C200DA5423 /* VoiceViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 08FB8767215DD1C200DA5423 /* VoiceViewController.m */; }; |
| 705 | 706 | 08FB876B215DD1C200DA5423 /* VoiceViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 08FB8767215DD1C200DA5423 /* VoiceViewController.m */; }; |
| ... | ... | @@ -1193,6 +1194,8 @@ |
| 1193 | 1194 | 08C090B421510E4B00E23D16 /* CNLiveTableViewCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CNLiveTableViewCell.m; sourceTree = "<group>"; }; |
| 1194 | 1195 | 08C090BD215116CB00E23D16 /* CNLiveIndicatorTableViewCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CNLiveIndicatorTableViewCell.h; sourceTree = "<group>"; }; |
| 1195 | 1196 | 08C090BE215116CB00E23D16 /* CNLiveIndicatorTableViewCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CNLiveIndicatorTableViewCell.m; sourceTree = "<group>"; }; |
| 1197 | + 08E7998D24B2F52C00F15587 /* WKWebView+JavaScript.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "WKWebView+JavaScript.h"; sourceTree = "<group>"; }; | |
| 1198 | + 08E7998E24B2F52C00F15587 /* WKWebView+JavaScript.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "WKWebView+JavaScript.m"; sourceTree = "<group>"; }; | |
| 1196 | 1199 | 08FB8767215DD1C200DA5423 /* VoiceViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = VoiceViewController.m; sourceTree = "<group>"; }; |
| 1197 | 1200 | 08FB8768215DD1C200DA5423 /* VoiceViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VoiceViewController.h; sourceTree = "<group>"; }; |
| 1198 | 1201 | 08FB876C215DD3C400DA5423 /* NewVoiceTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NewVoiceTableViewCell.m; sourceTree = "<group>"; }; |
| ... | ... | @@ -1340,13 +1343,15 @@ |
| 1340 | 1343 | 0838485A215CAB1C00186618 /* Html5Utils */ = { |
| 1341 | 1344 | isa = PBXGroup; |
| 1342 | 1345 | children = ( |
| 1346 | + 0838485E215CAB1C00186618 /* Html5Utils.h */, | |
| 1347 | + 0838485F215CAB1C00186618 /* UIColor+Change.h */, | |
| 1343 | 1348 | 0838485B215CAB1C00186618 /* UIColor+Change.m */, |
| 1344 | 1349 | 0838485C215CAB1C00186618 /* YWWebScrollView.h */, |
| 1350 | + 08384861215CAB1C00186618 /* YWWebScrollView.m */, | |
| 1345 | 1351 | 0838485D215CAB1C00186618 /* UIWebView+JavaScript.h */, |
| 1346 | - 0838485E215CAB1C00186618 /* Html5Utils.h */, | |
| 1347 | - 0838485F215CAB1C00186618 /* UIColor+Change.h */, | |
| 1348 | 1352 | 08384860215CAB1C00186618 /* UIWebView+JavaScript.m */, |
| 1349 | - 08384861215CAB1C00186618 /* YWWebScrollView.m */, | |
| 1353 | + 08E7998D24B2F52C00F15587 /* WKWebView+JavaScript.h */, | |
| 1354 | + 08E7998E24B2F52C00F15587 /* WKWebView+JavaScript.m */, | |
| 1350 | 1355 | ); |
| 1351 | 1356 | path = Html5Utils; |
| 1352 | 1357 | sourceTree = "<group>"; |
| ... | ... | @@ -3090,6 +3095,7 @@ |
| 3090 | 3095 | 08585CF3215B3AA900FC2CA2 /* NoticeTableViewCell.m in Sources */, |
| 3091 | 3096 | 085D86292159C72F0037D480 /* ZXVideoPlayerBatteryView.m in Sources */, |
| 3092 | 3097 | 08384862215CAB1C00186618 /* UIColor+Change.m in Sources */, |
| 3098 | + 08E7998F24B2F52C00F15587 /* WKWebView+JavaScript.m in Sources */, | |
| 3093 | 3099 | 08C08FF2214FA24F00E23D16 /* CNLiveBaseNavigationController.m in Sources */, |
| 3094 | 3100 | 08C09017215088B800E23D16 /* CNMyViewController.m in Sources */, |
| 3095 | 3101 | 084ACE0721547C0C0089E035 /* CNLiveCertificatesViewController.m in Sources */, | ... | ... |
CNLiveScioLive/Pages/Home/新闻详情/Controller/VideoDetailViewController.m
| ... | ... | @@ -10,8 +10,9 @@ |
| 10 | 10 | #import "CNHomeModel.h" |
| 11 | 11 | #import "CNLiveNewsLeftPicTableViewCell.h" |
| 12 | 12 | #import "CNLiveNewsDetailsView.h" |
| 13 | +#import <WebKit/WebKit.h> | |
| 13 | 14 | |
| 14 | -@interface VideoDetailViewController ()<UIWebViewDelegate,UITableViewDelegate,UITableViewDataSource>{ | |
| 15 | +@interface VideoDetailViewController ()<WKUIDelegate,WKNavigationDelegate,UITableViewDelegate,UITableViewDataSource>{ | |
| 15 | 16 | UIScrollView *_backgroundView; |
| 16 | 17 | NSURL *_movieUrl; |
| 17 | 18 | NSString *_url; |
| ... | ... | @@ -26,7 +27,7 @@ |
| 26 | 27 | |
| 27 | 28 | @property (copy, nonatomic) NSString *authorName; |
| 28 | 29 | @property (strong, nonatomic) NSMutableArray *data; |
| 29 | -@property (strong, nonatomic) UIWebView *webView; | |
| 30 | +@property (strong, nonatomic) WKWebView *webView; | |
| 30 | 31 | |
| 31 | 32 | @property (nonatomic, assign) NSInteger pageNo; |
| 32 | 33 | @property (nonatomic, assign) BOOL isRefreshing; |
| ... | ... | @@ -206,13 +207,13 @@ static NSString *newsLeftPicTableViewCell = @"CNLiveNewsLeftPicTableViewCell"; |
| 206 | 207 | } |
| 207 | 208 | if (0 == weakSelf.fontIndex) { |
| 208 | 209 | NSLog(@"0"); |
| 209 | - [weakSelf.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'"]; | |
| 210 | + [weakSelf.webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'" completionHandler:nil]; | |
| 210 | 211 | } else if (1 == weakSelf.fontIndex) { |
| 211 | 212 | NSLog(@"1"); |
| 212 | - [weakSelf.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '120%'"]; | |
| 213 | + [weakSelf.webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '120%'" completionHandler:nil]; | |
| 213 | 214 | } else if (2 == weakSelf.fontIndex) { |
| 214 | 215 | NSLog(@"2"); |
| 215 | - [weakSelf.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '140%'"]; | |
| 216 | + [weakSelf.webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '140%'" completionHandler:nil]; | |
| 216 | 217 | } |
| 217 | 218 | } |
| 218 | 219 | break; |
| ... | ... | @@ -408,16 +409,48 @@ static NSString *newsLeftPicTableViewCell = @"CNLiveNewsLeftPicTableViewCell"; |
| 408 | 409 | [super didReceiveMemoryWarning]; |
| 409 | 410 | // Dispose of any resources that can be recreated. |
| 410 | 411 | } |
| 411 | -#pragma mark - UIWebViewDelegate | |
| 412 | -- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ | |
| 413 | - NSURL *requestURL =[request URL]; | |
| 414 | - if (([[requestURL scheme] isEqualToString: @"http"] || [[requestURL scheme] isEqualToString:@"https"] || [[requestURL scheme] isEqualToString: @"mailto" ]) && (navigationType == UIWebViewNavigationTypeLinkClicked)) { | |
| 415 | - [[UIApplication sharedApplication] openURL:[request URL]]; | |
| 416 | - return NO; | |
| 412 | +#pragma mark - WebViewDelegate | |
| 413 | +- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { | |
| 414 | + NSURL *requestURL = webView.URL; | |
| 415 | + if (([[requestURL scheme] isEqualToString: @"http"] || [[requestURL scheme] isEqualToString:@"https"] || [[requestURL scheme] isEqualToString: @"mailto" ]) && (navigationAction.navigationType == WKNavigationTypeLinkActivated)) { | |
| 416 | + [[UIApplication sharedApplication] openURL:requestURL]; | |
| 417 | + decisionHandler(WKNavigationActionPolicyCancel); | |
| 417 | 418 | } |
| 418 | - return YES; | |
| 419 | -} | |
| 420 | -- (void)webViewDidFinishLoad:(UIWebView *)webView{ | |
| 419 | + decisionHandler(WKNavigationActionPolicyAllow); | |
| 420 | +} | |
| 421 | +//- (void)webViewDidFinishLoad:(UIWebView *)webView{ | |
| 422 | +// CGFloat width = MainScreenWidth - 20; | |
| 423 | +// NSString *string = [NSString stringWithFormat: | |
| 424 | +// @"var script = document.createElement('script');" | |
| 425 | +// "script.type = 'text/javascript';" | |
| 426 | +// "script.text = \"function ResizeImages() { " | |
| 427 | +// "var myimg,oldwidth;" | |
| 428 | +// "var maxwidth=%f;" //缩放系数 | |
| 429 | +// "for(i=0;i <document.images.length;i++){" | |
| 430 | +// "myimg = document.images[i];" | |
| 431 | +// "if(myimg.width > maxwidth){" | |
| 432 | +// "oldwidth = myimg.width;" | |
| 433 | +// "myimg.width = maxwidth;" | |
| 434 | +// "myimg.height = myimg.height * (maxwidth/oldwidth);" | |
| 435 | +// "}" | |
| 436 | +// "}" | |
| 437 | +// "}\";" | |
| 438 | +// "document.getElementsByTagName('head')[0].appendChild(script);",width]; | |
| 439 | +// [webView stringByEvaluatingJavaScriptFromString:string]; | |
| 440 | +// [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"]; | |
| 441 | +// CGRect frame = _webView.frame; | |
| 442 | +// frame.size.width = MainScreenWidth; | |
| 443 | +// frame.size.height = 1; | |
| 444 | +// _webView.frame = frame; | |
| 445 | +// CGFloat graphicLiveHeight = _webView.scrollView.contentSize.height; | |
| 446 | +// frame.size.height = _webView.scrollView.contentSize.height; | |
| 447 | +// _webView.frame = frame; | |
| 448 | +// if (0 == [_heightArr[_fontIndex] floatValue]) { | |
| 449 | +// [_heightArr replaceObjectAtIndex:_fontIndex withObject:[NSString stringWithFormat:@"%f",graphicLiveHeight]]; | |
| 450 | +// } | |
| 451 | +// [self loadRelatedInformation]; | |
| 452 | +//} | |
| 453 | +- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation*)navigation { | |
| 421 | 454 | CGFloat width = MainScreenWidth - 20; |
| 422 | 455 | NSString *string = [NSString stringWithFormat: |
| 423 | 456 | @"var script = document.createElement('script');" |
| ... | ... | @@ -435,18 +468,22 @@ static NSString *newsLeftPicTableViewCell = @"CNLiveNewsLeftPicTableViewCell"; |
| 435 | 468 | "}" |
| 436 | 469 | "}\";" |
| 437 | 470 | "document.getElementsByTagName('head')[0].appendChild(script);",width]; |
| 438 | - [webView stringByEvaluatingJavaScriptFromString:string]; | |
| 439 | - [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"]; | |
| 440 | - CGRect frame = _webView.frame; | |
| 441 | - frame.size.width = MainScreenWidth; | |
| 442 | - frame.size.height = 1; | |
| 443 | - _webView.frame = frame; | |
| 444 | - CGFloat graphicLiveHeight = _webView.scrollView.contentSize.height; | |
| 445 | - frame.size.height = _webView.scrollView.contentSize.height; | |
| 446 | - _webView.frame = frame; | |
| 447 | - if (0 == [_heightArr[_fontIndex] floatValue]) { | |
| 448 | - [_heightArr replaceObjectAtIndex:_fontIndex withObject:[NSString stringWithFormat:@"%f",graphicLiveHeight]]; | |
| 449 | - } | |
| 471 | + [webView evaluateJavaScript:@"ResizeImages();" completionHandler:nil]; | |
| 472 | + [webView evaluateJavaScript:string completionHandler:^(id _Nullable response, NSError * _Nullable error) { | |
| 473 | + NSLog(@"value: %@ error: %@", response, error); | |
| 474 | + dispatch_async(dispatch_get_main_queue(), ^{ | |
| 475 | + CGRect frame = self->_webView.frame; | |
| 476 | + frame.size.width = MainScreenWidth; | |
| 477 | + frame.size.height = 1; | |
| 478 | + self->_webView.frame = frame; | |
| 479 | + CGFloat graphicLiveHeight = self->_webView.scrollView.contentSize.height; | |
| 480 | + frame.size.height = self->_webView.scrollView.contentSize.height; | |
| 481 | + self->_webView.frame = frame; | |
| 482 | + if (0 == [self->_heightArr[self->_fontIndex] floatValue]) { | |
| 483 | + [self->_heightArr replaceObjectAtIndex:self->_fontIndex withObject:[NSString stringWithFormat:@"%f",graphicLiveHeight]]; | |
| 484 | + } | |
| 485 | + }); | |
| 486 | + }]; | |
| 450 | 487 | [self loadRelatedInformation]; |
| 451 | 488 | } |
| 452 | 489 | |
| ... | ... | @@ -535,14 +572,23 @@ static NSString *newsLeftPicTableViewCell = @"CNLiveNewsLeftPicTableViewCell"; |
| 535 | 572 | [self.navigationController popToRootViewControllerAnimated:YES]; |
| 536 | 573 | } |
| 537 | 574 | #pragma mark - Lazy Laoding |
| 538 | -- (UIWebView *)webView { | |
| 575 | +- (WKWebView *)webView { | |
| 539 | 576 | if (!_webView) { |
| 540 | - _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, 0)]; | |
| 577 | + NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"; | |
| 578 | + | |
| 579 | + WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES]; | |
| 580 | + WKUserContentController *wkUController = [[WKUserContentController alloc] init]; | |
| 581 | + [wkUController addUserScript:wkUScript]; | |
| 582 | + | |
| 583 | + WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init]; | |
| 584 | + wkWebConfig.userContentController = wkUController; | |
| 585 | + _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, 0) configuration:wkWebConfig]; | |
| 541 | 586 | _webView.scrollView.backgroundColor = [UIColor clearColor]; |
| 542 | - _webView.dataDetectorTypes = UIDataDetectorTypeNone; | |
| 587 | +// _webView.dataDetectorTypes = UIDataDetectorTypeNone; | |
| 543 | 588 | _webView.backgroundColor = [UIColor clearColor]; |
| 544 | 589 | _webView.scrollView.bounces = NO; |
| 545 | - _webView.delegate = self; | |
| 590 | + _webView.navigationDelegate = self; | |
| 591 | + _webView.UIDelegate = self; | |
| 546 | 592 | _webView.backgroundColor = [UIColor whiteColor]; |
| 547 | 593 | _webView.opaque = NO; |
| 548 | 594 | _webView.scrollView.scrollEnabled = NO; | ... | ... |
CNLiveScioLive/Pages/Home/新闻详情/OldController/OldArticleDetailViewController.h
| ... | ... | @@ -7,7 +7,9 @@ |
| 7 | 7 | // |
| 8 | 8 | |
| 9 | 9 | #import "CNLiveBaseViewController.h" |
| 10 | -@interface OldArticleDetailViewController : CNLiveBaseViewController<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource,UIWebViewDelegate>{ | |
| 10 | +#import <WebKit/WebKit.h> | |
| 11 | + | |
| 12 | +@interface OldArticleDetailViewController : CNLiveBaseViewController<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource,WKNavigationDelegate, WKUIDelegate>{ | |
| 11 | 13 | NSArray *_newsArray; |
| 12 | 14 | NSDictionary *_dicArticle; |
| 13 | 15 | NSDictionary *_dictionary; |
| ... | ... | @@ -18,7 +20,7 @@ |
| 18 | 20 | } |
| 19 | 21 | @property (strong, nonatomic) UILabel *errorLabe; |
| 20 | 22 | @property (strong, nonatomic) NSArray *newsArray; |
| 21 | -@property (strong, nonatomic) UIWebView *webView; | |
| 23 | +@property (strong, nonatomic) WKWebView *webView; | |
| 22 | 24 | @property (strong, nonatomic) UITableView *tableView; |
| 23 | 25 | @property (strong, nonatomic) UIScrollView *scrollView; |
| 24 | 26 | -(id)initWithDictionary:(NSDictionary*)dictionary; | ... | ... |
CNLiveScioLive/Pages/Home/新闻详情/OldController/OldArticleDetailViewController.m
| ... | ... | @@ -11,14 +11,10 @@ |
| 11 | 11 | |
| 12 | 12 | #import "VideoTableViewCell.h" |
| 13 | 13 | #import "ArticleTableViewCell.h" |
| 14 | -#import "NJKWebViewProgress.h" | |
| 15 | -#import "NJKWebViewProgressView.h" | |
| 16 | 14 | #import "YWWebScrollView.h" |
| 17 | 15 | #import "Html5Utils.h" |
| 18 | 16 | |
| 19 | -@interface OldArticleDetailViewController ()<NJKWebViewProgressDelegate,UIGestureRecognizerDelegate>{ | |
| 20 | - NJKWebViewProgressView *_progressView; | |
| 21 | - NJKWebViewProgress *_progressProxy; | |
| 17 | +@interface OldArticleDetailViewController ()<UIGestureRecognizerDelegate>{ | |
| 22 | 18 | NSMutableArray *_imageArray; |
| 23 | 19 | NSInteger typeFlag; |
| 24 | 20 | NSString *htmlString; |
| ... | ... | @@ -190,13 +186,13 @@ |
| 190 | 186 | } |
| 191 | 187 | if (0 == fontIndex) { |
| 192 | 188 | NSLog(@"0"); |
| 193 | - [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'"]; | |
| 189 | + [_webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'" completionHandler:nil]; | |
| 194 | 190 | } else if (1 == fontIndex) { |
| 195 | 191 | NSLog(@"1"); |
| 196 | - [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '120%'"]; | |
| 192 | + [_webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '120%'" completionHandler:nil]; | |
| 197 | 193 | } else if (2 == fontIndex) { |
| 198 | 194 | NSLog(@"2"); |
| 199 | - [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '140%'"]; | |
| 195 | + [_webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '140%'" completionHandler:nil]; | |
| 200 | 196 | } |
| 201 | 197 | } else if (20000 == btn.tag) { |
| 202 | 198 | TableData *notice = [[TableData alloc] initWithJSONDictionary:_dictionary]; |
| ... | ... | @@ -255,26 +251,37 @@ |
| 255 | 251 | // [self.scrollView setFooter:footer]; |
| 256 | 252 | [self.view addSubview:self.scrollView]; |
| 257 | 253 | |
| 258 | - _progressProxy = [[NJKWebViewProgress alloc] init]; | |
| 259 | - _progressProxy.webViewProxyDelegate = self; | |
| 260 | - _progressProxy.progressDelegate = self; | |
| 254 | +// _progressProxy = [[NJKWebViewProgress alloc] init]; | |
| 255 | +// _progressProxy.webViewProxyDelegate = self; | |
| 256 | +// _progressProxy.progressDelegate = self; | |
| 261 | 257 | |
| 262 | - CGFloat progressBarHeight = 2.f; | |
| 263 | - CGRect navigationBarBounds = self.navigationController.navigationBar.bounds; | |
| 264 | - CGRect barFrame = CGRectMake(0, navigationBarBounds.size.height, navigationBarBounds.size.width, progressBarHeight); | |
| 265 | - _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame]; | |
| 266 | - _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin; | |
| 258 | +// CGFloat progressBarHeight = 2.f; | |
| 259 | +// CGRect navigationBarBounds = self.navigationController.navigationBar.bounds; | |
| 260 | +// CGRect barFrame = CGRectMake(0, navigationBarBounds.size.height, navigationBarBounds.size.width, progressBarHeight); | |
| 261 | +// _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame]; | |
| 262 | +// _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin; | |
| 263 | + | |
| 264 | + NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"; | |
| 265 | + | |
| 266 | + WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES]; | |
| 267 | + WKUserContentController *wkUController = [[WKUserContentController alloc] init]; | |
| 268 | + [wkUController addUserScript:wkUScript]; | |
| 269 | + | |
| 270 | + WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init]; | |
| 271 | + wkWebConfig.userContentController = wkUController; | |
| 267 | 272 | |
| 268 | 273 | if (iPhoneX) { |
| 269 | - _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, MainScreenHeight - 88 - 44 - 34)]; | |
| 274 | + _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, MainScreenHeight - 88 - 44 - 34) configuration:wkWebConfig]; | |
| 270 | 275 | } else { |
| 271 | - _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, MainScreenHeight - 64 - 44)]; | |
| 276 | + _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, MainScreenHeight - 64 - 44) configuration:wkWebConfig]; | |
| 272 | 277 | } |
| 273 | 278 | _webView.scrollView.backgroundColor = [UIColor whiteColor]; |
| 274 | - _webView.dataDetectorTypes = UIDataDetectorTypeNone; | |
| 279 | +// _webView.dataDetectorTypes = UIDataDetectorTypeNone; | |
| 275 | 280 | _webView.backgroundColor = [UIColor whiteColor]; |
| 276 | - _webView.delegate = _progressProxy; | |
| 277 | - _webView.scalesPageToFit = YES; | |
| 281 | + _webView.navigationDelegate = self; | |
| 282 | + _webView.UIDelegate = self; | |
| 283 | +// _webView.delegate = _progressProxy; | |
| 284 | +// _webView.scalesPageToFit = YES; | |
| 278 | 285 | _webView.opaque = NO; |
| 279 | 286 | [self.scrollView addSubview:_webView]; |
| 280 | 287 | |
| ... | ... | @@ -310,7 +317,6 @@ |
| 310 | 317 | |
| 311 | 318 | - (void)viewWillAppear:(BOOL)animated { |
| 312 | 319 | [super viewWillAppear:animated]; |
| 313 | - [self.navigationController.navigationBar addSubview:_progressView]; | |
| 314 | 320 | |
| 315 | 321 | NSError *error = nil; |
| 316 | 322 | [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: &error]; |
| ... | ... | @@ -360,48 +366,20 @@ |
| 360 | 366 | } |
| 361 | 367 | } |
| 362 | 368 | |
| 363 | --(void)viewWillDisappear:(BOOL)animated { | |
| 369 | +- (void)viewWillDisappear:(BOOL)animated { | |
| 364 | 370 | [super viewWillDisappear:animated]; |
| 365 | - [_progressView removeFromSuperview]; | |
| 366 | 371 | } |
| 367 | 372 | |
| 368 | 373 | #pragma mark - WebView加载H5页面区域 |
| 369 | - | |
| 370 | --(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress { | |
| 371 | - [_progressView setProgress:progress animated:YES]; | |
| 372 | - NSLog(@"%f",progress); | |
| 373 | - if (progress >= 1.0) { | |
| 374 | - CGRect frame = _webView.frame; | |
| 375 | - frame.size.width = MainScreenWidth; | |
| 376 | - frame.size.height = 1; | |
| 377 | - _webView.frame = frame; | |
| 378 | - CGFloat graphicLiveHeight = _webView.scrollView.contentSize.height; | |
| 379 | - frame.size.height = _webView.scrollView.contentSize.height; | |
| 380 | - _webView.frame = frame; | |
| 381 | - | |
| 382 | - if (0 == [heightArr[fontIndex] floatValue]) { | |
| 383 | - [heightArr replaceObjectAtIndex:fontIndex withObject:[NSString stringWithFormat:@"%f",graphicLiveHeight]]; | |
| 384 | - } | |
| 385 | - [self.scrollView.mj_header endRefreshing]; | |
| 386 | - [_webView setFrame:CGRectMake(0, 0, MainScreenWidth, [heightArr[fontIndex] floatValue])]; | |
| 387 | - [self.scrollView setContentSize:CGSizeMake(MainScreenWidth, [heightArr[fontIndex] floatValue])]; | |
| 388 | - if (!hasLoad) { | |
| 389 | - [self loadProgramRelativeData]; | |
| 390 | - } | |
| 391 | - } | |
| 392 | -} | |
| 393 | - | |
| 394 | -- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { | |
| 395 | - | |
| 374 | +- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { | |
| 396 | 375 | if (_imageArray.count) { |
| 397 | - NSURL *requestURL = [request URL]; | |
| 376 | + NSURL *requestURL = webView.URL; | |
| 398 | 377 | BOOL isImg = [@"img" isEqualToString:[requestURL scheme]]; |
| 399 | 378 | BOOL isHttp = [[requestURL scheme] isEqualToString: @"http"]; |
| 400 | 379 | BOOL isHttps = [[requestURL scheme] isEqualToString: @"https"]; |
| 401 | 380 | BOOL isMailto = [[requestURL scheme] isEqualToString: @"mailto"]; |
| 402 | - | |
| 403 | 381 | if (isImg) { |
| 404 | - NSString *requesturl = [[NSString stringWithFormat:@"%@",request.URL] substringFromIndex:4]; | |
| 382 | + NSString *requesturl = [[NSString stringWithFormat:@"%@",webView.URL] substringFromIndex:4]; | |
| 405 | 383 | for (NSInteger indes = 0; indes < _imageArray.count; indes++) { |
| 406 | 384 | NSString *url = [NSString stringWithFormat:@"%@",_imageArray[indes]]; |
| 407 | 385 | if ([url isEqualToString:requesturl]) { |
| ... | ... | @@ -409,24 +387,21 @@ |
| 409 | 387 | break; |
| 410 | 388 | } |
| 411 | 389 | } |
| 412 | - return NO; | |
| 413 | 390 | } else if (isHttp || isHttps || isMailto) { |
| 414 | - [[UIApplication sharedApplication] openURL:[request URL]]; | |
| 415 | - return NO; | |
| 391 | + [[UIApplication sharedApplication] openURL:webView.URL]; | |
| 392 | + decisionHandler(WKNavigationActionPolicyCancel); | |
| 416 | 393 | } |
| 417 | 394 | } else { |
| 418 | - NSURL *requestURL =[request URL]; | |
| 419 | - if (([[requestURL scheme] isEqualToString: @"http"] || [[requestURL scheme] isEqualToString:@"https"] || [[requestURL scheme] isEqualToString: @"mailto" ]) && (navigationType == UIWebViewNavigationTypeLinkClicked)) { | |
| 420 | - [[UIApplication sharedApplication] openURL:[request URL]]; | |
| 421 | - return NO; | |
| 395 | + NSURL *requestURL = webView.URL; | |
| 396 | + if (([[requestURL scheme] isEqualToString: @"http"] || [[requestURL scheme] isEqualToString:@"https"] || [[requestURL scheme] isEqualToString: @"mailto" ]) && (navigationAction.navigationType == WKNavigationTypeLinkActivated)) { | |
| 397 | + [[UIApplication sharedApplication] openURL:webView.URL]; | |
| 398 | + decisionHandler(WKNavigationActionPolicyCancel); | |
| 422 | 399 | } |
| 423 | 400 | } |
| 424 | - return YES; | |
| 401 | + decisionHandler(WKNavigationActionPolicyAllow); | |
| 425 | 402 | } |
| 426 | -- (void)webViewDidStartLoad:(UIWebView *)webView{ | |
| 427 | - NSLog(@"did start"); | |
| 428 | -} | |
| 429 | -- (void)webViewDidFinishLoad:(UIWebView *)webView{ | |
| 403 | + | |
| 404 | +- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation*)navigation { | |
| 430 | 405 | _imageArray = [_webView getImgs]; |
| 431 | 406 | NSMutableArray *array = [NSMutableArray array]; |
| 432 | 407 | for (NSString *string in _imageArray) { |
| ... | ... | @@ -438,20 +413,38 @@ |
| 438 | 413 | [webView addClickEventOnImg];//有的图片没有点击事件 |
| 439 | 414 | |
| 440 | 415 | //使页面中图片width=100% |
| 441 | - [self.webView stringByEvaluatingJavaScriptFromString: | |
| 416 | + [self.webView evaluateJavaScript: | |
| 442 | 417 | @"var script = document.createElement('script');" |
| 443 | 418 | "script.type = 'text/javascript';" |
| 444 | 419 | "script.text = \"var imgs = document.getElementById('article_content').getElementsByTagName('img')\";" |
| 445 | 420 | "for(var i=0; i<imgs.length; i++){" |
| 446 | 421 | "imgs[i].style.width = '100%';" |
| 447 | 422 | "}" |
| 448 | - "document.getElementsByTagName('body')[0].appendChild(script);"]; | |
| 423 | + "document.getElementsByTagName('body')[0].appendChild(script);" completionHandler:nil]; | |
| 449 | 424 | |
| 450 | 425 | //通过css隐藏h5页面中头部和底部的logo |
| 451 | 426 | NSString *cssString = @".container { display:none !important; }"; |
| 452 | 427 | NSString *javascriptString = @"var style = document.createElement('style'); style.innerHTML = '%@'; document.head.appendChild(style)"; |
| 453 | 428 | NSString *javascriptWithCSSString = [NSString stringWithFormat:javascriptString, cssString]; |
| 454 | - [webView stringByEvaluatingJavaScriptFromString:javascriptWithCSSString]; | |
| 429 | + [webView evaluateJavaScript:javascriptWithCSSString completionHandler:nil]; | |
| 430 | + | |
| 431 | + CGRect frame = _webView.frame; | |
| 432 | + frame.size.width = MainScreenWidth; | |
| 433 | + frame.size.height = 1; | |
| 434 | + _webView.frame = frame; | |
| 435 | + CGFloat graphicLiveHeight = _webView.scrollView.contentSize.height; | |
| 436 | + frame.size.height = _webView.scrollView.contentSize.height; | |
| 437 | + _webView.frame = frame; | |
| 438 | + | |
| 439 | + if (0 == [heightArr[fontIndex] floatValue]) { | |
| 440 | + [heightArr replaceObjectAtIndex:fontIndex withObject:[NSString stringWithFormat:@"%f",graphicLiveHeight]]; | |
| 441 | + } | |
| 442 | + [self.scrollView.mj_header endRefreshing]; | |
| 443 | + [_webView setFrame:CGRectMake(0, 0, MainScreenWidth, [heightArr[fontIndex] floatValue])]; | |
| 444 | + [self.scrollView setContentSize:CGSizeMake(MainScreenWidth, [heightArr[fontIndex] floatValue])]; | |
| 445 | + if (!hasLoad) { | |
| 446 | + [self loadProgramRelativeData]; | |
| 447 | + } | |
| 455 | 448 | } |
| 456 | 449 | |
| 457 | 450 | -(void)pushPicController:(NSString *)url withPoint:(NSInteger)CurrentIndex { | ... | ... |
CNLiveScioLive/Pages/Home/新闻详情/OldController/OldVideoDetailViewController.h
| ... | ... | @@ -10,15 +10,16 @@ |
| 10 | 10 | #import "CNLiveMyCollectionViewController.h" |
| 11 | 11 | #import "CNLivePlayerController.h" |
| 12 | 12 | #import "BaseViewController.h" |
| 13 | +#import <WebKit/WebKit.h> | |
| 13 | 14 | |
| 14 | -@interface OldVideoDetailViewController : BaseViewController<UITableViewDelegate,UITableViewDataSource,UIWebViewDelegate>{ | |
| 15 | +@interface OldVideoDetailViewController : BaseViewController<UITableViewDelegate,UITableViewDataSource,WKUIDelegate,WKNavigationDelegate>{ | |
| 15 | 16 | UIScrollView *backgroundView; |
| 16 | 17 | UIImageView *imageView; |
| 17 | 18 | UIButton *playBtn; |
| 18 | 19 | NSURL *movieUrl; |
| 19 | 20 | NSString *url; |
| 20 | 21 | UIImage *bgImg; |
| 21 | - UIWebView *_webView; | |
| 22 | + WKWebView *_webView; | |
| 22 | 23 | NSArray *_newsArray; |
| 23 | 24 | CGFloat videoHeight; |
| 24 | 25 | NSDictionary *_dictionary; | ... | ... |
CNLiveScioLive/Pages/Home/新闻详情/OldController/OldVideoDetailViewController.m
| ... | ... | @@ -201,13 +201,24 @@ |
| 201 | 201 | fontIndex = (int)[[NSUserDefaults standardUserDefaults] integerForKey:@"FontSize"]; |
| 202 | 202 | heightArr = [NSMutableArray arrayWithArray:@[@"0",@"0",@"0"]]; |
| 203 | 203 | fontArray = @[@"16",@"20",@"24"]; |
| 204 | + | |
| 205 | + NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"; | |
| 206 | + | |
| 207 | + WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES]; | |
| 208 | + WKUserContentController *wkUController = [[WKUserContentController alloc] init]; | |
| 209 | + [wkUController addUserScript:wkUScript]; | |
| 210 | + | |
| 211 | + WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init]; | |
| 212 | + wkWebConfig.userContentController = wkUController; | |
| 204 | 213 | |
| 205 | - _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, 0)]; | |
| 214 | + _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, 0) configuration:wkWebConfig]; | |
| 206 | 215 | _webView.scrollView.backgroundColor = [UIColor clearColor]; |
| 207 | - _webView.dataDetectorTypes = UIDataDetectorTypeNone; | |
| 216 | +// _webView.dataDetectorTypes = UIDataDetectorTypeNone; | |
| 208 | 217 | _webView.backgroundColor = [UIColor clearColor]; |
| 209 | 218 | _webView.scrollView.bounces = NO; |
| 210 | - _webView.delegate = self; | |
| 219 | +// _webView.delegate = self; | |
| 220 | + _webView.navigationDelegate = self; | |
| 221 | + _webView.UIDelegate = self; | |
| 211 | 222 | _webView.opaque = NO; |
| 212 | 223 | [backgroundView addSubview:_webView]; |
| 213 | 224 | [_webView loadHTMLString:[self setHTMLString] baseURL:nil]; |
| ... | ... | @@ -366,15 +377,15 @@ |
| 366 | 377 | video.title = _dictionary[@"title"]; |
| 367 | 378 | self.videoController.video = video; |
| 368 | 379 | } |
| 369 | -- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ | |
| 370 | - NSURL *requestURL =[request URL]; | |
| 371 | - if (([[requestURL scheme] isEqualToString: @"http"] || [[requestURL scheme] isEqualToString:@"https"] || [[requestURL scheme] isEqualToString: @"mailto" ]) && (navigationType == UIWebViewNavigationTypeLinkClicked)) { | |
| 372 | - [[UIApplication sharedApplication] openURL:[request URL]]; | |
| 373 | - return NO; | |
| 380 | +- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { | |
| 381 | + NSURL *requestURL = webView.URL; | |
| 382 | + if (([[requestURL scheme] isEqualToString: @"http"] || [[requestURL scheme] isEqualToString:@"https"] || [[requestURL scheme] isEqualToString: @"mailto" ]) && (navigationAction.navigationType == WKNavigationTypeLinkActivated)) { | |
| 383 | + [[UIApplication sharedApplication] openURL:requestURL]; | |
| 384 | + decisionHandler(WKNavigationActionPolicyCancel); | |
| 374 | 385 | } |
| 375 | - return YES; | |
| 386 | + decisionHandler(WKNavigationActionPolicyAllow); | |
| 376 | 387 | } |
| 377 | -- (void)webViewDidFinishLoad:(UIWebView *)webView{ | |
| 388 | +- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation*)navigation { | |
| 378 | 389 | CGFloat width = MainScreenWidth - 20; |
| 379 | 390 | NSString *string = [NSString stringWithFormat: |
| 380 | 391 | @"var script = document.createElement('script');" |
| ... | ... | @@ -392,18 +403,22 @@ |
| 392 | 403 | "}" |
| 393 | 404 | "}\";" |
| 394 | 405 | "document.getElementsByTagName('head')[0].appendChild(script);",width]; |
| 395 | - [webView stringByEvaluatingJavaScriptFromString:string]; | |
| 396 | - [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"]; | |
| 397 | - CGRect frame = _webView.frame; | |
| 398 | - frame.size.width = MainScreenWidth; | |
| 399 | - frame.size.height = 1; | |
| 400 | - _webView.frame = frame; | |
| 401 | - CGFloat graphicLiveHeight = _webView.scrollView.contentSize.height; | |
| 402 | - frame.size.height = _webView.scrollView.contentSize.height; | |
| 403 | - _webView.frame = frame; | |
| 404 | - if (0 == [heightArr[fontIndex] floatValue]) { | |
| 405 | - [heightArr replaceObjectAtIndex:fontIndex withObject:[NSString stringWithFormat:@"%f",graphicLiveHeight]]; | |
| 406 | - } | |
| 406 | + [webView evaluateJavaScript:@"ResizeImages();" completionHandler:nil]; | |
| 407 | + [webView evaluateJavaScript:string completionHandler:^(id _Nullable response, NSError * _Nullable error) { | |
| 408 | + NSLog(@"value: %@ error: %@", response, error); | |
| 409 | + dispatch_async(dispatch_get_main_queue(), ^{ | |
| 410 | + CGRect frame = self->_webView.frame; | |
| 411 | + frame.size.width = MainScreenWidth; | |
| 412 | + frame.size.height = 1; | |
| 413 | + self->_webView.frame = frame; | |
| 414 | + CGFloat graphicLiveHeight = self->_webView.scrollView.contentSize.height; | |
| 415 | + frame.size.height = self->_webView.scrollView.contentSize.height; | |
| 416 | + self->_webView.frame = frame; | |
| 417 | + if (0 == [self->heightArr[self->fontIndex] floatValue]) { | |
| 418 | + [self->heightArr replaceObjectAtIndex:self->fontIndex withObject:[NSString stringWithFormat:@"%f",graphicLiveHeight]]; | |
| 419 | + } | |
| 420 | + }); | |
| 421 | + }]; | |
| 407 | 422 | [self loadRelatedInformation]; |
| 408 | 423 | } |
| 409 | 424 | - (void)loadRelatedInformation { |
| ... | ... | @@ -447,8 +462,6 @@ |
| 447 | 462 | [AlertViewTool showHUDAddedToView:self.view text:CustomStr(@"ServerBusy")]; |
| 448 | 463 | }]; |
| 449 | 464 | } |
| 450 | -- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{ | |
| 451 | -} | |
| 452 | 465 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
| 453 | 466 | return [_newsArray count]; |
| 454 | 467 | } | ... | ... |
CNLiveScioLive/Pages/Live/View/CNLiveIndicatorTableViewCell.m
| ... | ... | @@ -23,7 +23,7 @@ |
| 23 | 23 | { |
| 24 | 24 | self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; |
| 25 | 25 | if (self) { |
| 26 | - self.accessoryType = UITableViewCellAccessoryDisclosureIndicator; | |
| 26 | +// self.accessoryType = UITableViewCellAccessoryDisclosureIndicator; | |
| 27 | 27 | _titleLabel = [[UILabel alloc] init]; |
| 28 | 28 | _titleLabel.numberOfLines = 2; |
| 29 | 29 | _titleLabel.lineBreakMode = NSLineBreakByTruncatingTail; | ... | ... |
CNLiveScioLive/Pages/SDK/Html5Utils/Html5Utils.h
CNLiveScioLive/Pages/SDK/Html5Utils/WKWebView+JavaScript.h
0 → 100644
| 1 | +// | |
| 2 | +// WKWebView+JavaScript.h | |
| 3 | +// CNLiveScioLive | |
| 4 | +// | |
| 5 | +// Created by CNLive-zxw on 2020/7/6. | |
| 6 | +// Copyright © 2020 CNLive. All rights reserved. | |
| 7 | +// | |
| 8 | + | |
| 9 | +#import <WebKit/WebKit.h> | |
| 10 | + | |
| 11 | +NS_ASSUME_NONNULL_BEGIN | |
| 12 | + | |
| 13 | +@interface WKWebView (JavaScript) | |
| 14 | +/// 获取某个标签的结点个数 | |
| 15 | +- (int)nodeCountOfTag:(NSString *)tag; | |
| 16 | + | |
| 17 | +/// 获取当前页面URL | |
| 18 | +- (NSString *)getCurrentURL; | |
| 19 | + | |
| 20 | +/// 获取标题 | |
| 21 | +- (NSString *)getTitle; | |
| 22 | + | |
| 23 | +/// 获取文章内容 | |
| 24 | +- (NSString *)getContent; | |
| 25 | + | |
| 26 | +/// 获取图片 | |
| 27 | +- (NSMutableArray *)getImgs; | |
| 28 | + | |
| 29 | +/// 获取当前页面所有链接 | |
| 30 | +- (NSArray *)getOnClicks; | |
| 31 | + | |
| 32 | +/// 改变背景颜色 | |
| 33 | +- (void)setBackgroundColor:(UIColor *)color; | |
| 34 | + | |
| 35 | +/// 为所有图片添加点击事件(网页中有些图片添加无效) | |
| 36 | +- (void)addClickEventOnImg; | |
| 37 | + | |
| 38 | +/// 改变指定标签的字体颜色 | |
| 39 | +- (void)setFontColor:(UIColor *)color withTag:(NSString *)tagName; | |
| 40 | + | |
| 41 | +/// 改变指定标签的字体大小 | |
| 42 | +- (void)setFontSize:(int)size withTag:(NSString *)tagName; | |
| 43 | + | |
| 44 | +/// 设置网页没有拷贝粘贴 | |
| 45 | +- (void)disableTouchCallout; | |
| 46 | +@end | |
| 47 | + | |
| 48 | +NS_ASSUME_NONNULL_END | ... | ... |
CNLiveScioLive/Pages/SDK/Html5Utils/WKWebView+JavaScript.m
0 → 100644
| 1 | +// | |
| 2 | +// WKWebView+JavaScript.m | |
| 3 | +// CNLiveScioLive | |
| 4 | +// | |
| 5 | +// Created by CNLive-zxw on 2020/7/6. | |
| 6 | +// Copyright © 2020 CNLive. All rights reserved. | |
| 7 | +// | |
| 8 | + | |
| 9 | +#import "WKWebView+JavaScript.h" | |
| 10 | +#import "UIColor+Change.h" | |
| 11 | +#define HTML_STRING_NIL_TO_NONE_IF(x) [WKWebView STRING_NIL_TO_NONE:x] | |
| 12 | + | |
| 13 | +@implementation WKWebView (JavaScript) | |
| 14 | + | |
| 15 | ++ (NSString *)STRING_NIL_TO_NONE:(NSString *)x { | |
| 16 | + if (x == nil || ([x isKindOfClass:[NSNull class]]) || ([x isKindOfClass:[NSString class]]&&x.length<=0) || [x isEqual:NULL]){ | |
| 17 | + return @""; | |
| 18 | + } else { | |
| 19 | + return [NSString stringWithFormat:@"%@",x]; | |
| 20 | + } | |
| 21 | +} | |
| 22 | + | |
| 23 | +/// 获取某个标签的结点个数 | |
| 24 | +- (int)nodeCountOfTag:(NSString *)tag { | |
| 25 | + __block int len = 0; | |
| 26 | + [self evaluateJavaScript:@"document.getElementsByTagName('%@').length" completionHandler:^(id _Nullable response, NSError * _Nullable error) { | |
| 27 | + NSLog(@"value: %@ error: %@", response, error); | |
| 28 | + len = [response intValue]; | |
| 29 | + }]; | |
| 30 | + return len; | |
| 31 | + | |
| 32 | +// NSString *jsString = [NSString stringWithFormat:@"document.getElementsByTagName('%@').length", tag]; | |
| 33 | +// | |
| 34 | +// int len = [[self evaluateJavaScript:jsString completionHandler:nil] intValue]; | |
| 35 | +// return len; | |
| 36 | +} | |
| 37 | + | |
| 38 | +/// 获取当前页面URL | |
| 39 | +- (NSString *)getCurrentURL { | |
| 40 | +// return HTML_STRING_NIL_TO_NONE_IF([self evaluateJavaScript:@"document.location.href" completionHandler:nil]); | |
| 41 | + __block NSString *result = @""; | |
| 42 | + [self evaluateJavaScript:@"document.location.href" completionHandler:^(id _Nullable response, NSError * _Nullable error) { | |
| 43 | + NSLog(@"value: %@ error: %@", response, error); | |
| 44 | + result = HTML_STRING_NIL_TO_NONE_IF(response); | |
| 45 | + }]; | |
| 46 | + return result; | |
| 47 | +} | |
| 48 | + | |
| 49 | +/// 获取标题 | |
| 50 | +- (NSString *)getTitle { | |
| 51 | + __block NSString *result = @""; | |
| 52 | + [self evaluateJavaScript:@"document.title" completionHandler:^(id _Nullable response, NSError * _Nullable error) { | |
| 53 | + NSLog(@"value: %@ error: %@", response, error); | |
| 54 | + result = HTML_STRING_NIL_TO_NONE_IF(response); | |
| 55 | + }]; | |
| 56 | + return result; | |
| 57 | +// return HTML_STRING_NIL_TO_NONE_IF([self evaluateJavaScript:@"document.title" completionHandler:nil]); | |
| 58 | +} | |
| 59 | + | |
| 60 | +/// 获取文章内容 | |
| 61 | +- (NSString *)getContent{ | |
| 62 | + __block NSString *result = @""; | |
| 63 | + [self evaluateJavaScript:@"document.documentElement.innerText" completionHandler:^(id _Nullable response, NSError * _Nullable error) { | |
| 64 | + NSLog(@"value: %@ error: %@", response, error); | |
| 65 | +// if (!result) { | |
| 66 | +// result = [self evaluateJavaScript:@"document.body.innerText" completionHandler:nil]; | |
| 67 | +// } | |
| 68 | + result = HTML_STRING_NIL_TO_NONE_IF(result); | |
| 69 | + }]; | |
| 70 | + return result; | |
| 71 | + | |
| 72 | +// NSString *result = [self evaluateJavaScript:@"document.documentElement.innerText" completionHandler:nil]; | |
| 73 | +// if (!result) { | |
| 74 | +// result = [self evaluateJavaScript:@"document.body.innerText" completionHandler:nil]; | |
| 75 | +// } | |
| 76 | +// result = HTML_STRING_NIL_TO_NONE_IF(result); | |
| 77 | +// return result; | |
| 78 | +} | |
| 79 | + | |
| 80 | +/// 获取所有图片链接 | |
| 81 | +- (NSMutableArray *)getImgs { | |
| 82 | + __block NSMutableArray *arrImgURL = [[NSMutableArray alloc] init]; | |
| 83 | + int node = [self nodeCountOfTag:@"img"]; | |
| 84 | + for (int i = 0; i < node; i++) { | |
| 85 | + NSString *jsString = [NSString stringWithFormat:@"document.getElementsByTagName('img')[%d].src", i]; | |
| 86 | +// NSString *img = [self evaluateJavaScript:jsString completionHandler:nil]; | |
| 87 | + [self evaluateJavaScript:jsString completionHandler:^(id _Nullable response, NSError * _Nullable error) { | |
| 88 | + NSLog(@"value: %@ error: %@", response, error); | |
| 89 | + [arrImgURL addObject:response]; | |
| 90 | + }]; | |
| 91 | +// [arrImgURL addObject:img]; | |
| 92 | + } | |
| 93 | + return arrImgURL; | |
| 94 | +} | |
| 95 | + | |
| 96 | +/// 获取当前页面所有点击链接 | |
| 97 | +- (NSArray *)getOnClicks { | |
| 98 | + __block NSMutableArray *arrOnClicks = [[NSMutableArray alloc] init]; | |
| 99 | + int node = [self nodeCountOfTag:@"a"]; | |
| 100 | + for (int i = 0; i < node; i++) { | |
| 101 | + NSString *jsString = [NSString stringWithFormat:@"document.getElementsByTagName('a')[%d].getAttribute('onclick')", i]; | |
| 102 | +// NSString *clickString = [self evaluateJavaScript:jsString completionHandler:nil]; | |
| 103 | + [self evaluateJavaScript:jsString completionHandler:^(id _Nullable response, NSError * _Nullable error) { | |
| 104 | + NSLog(@"value: %@ error: %@", response, error); | |
| 105 | + [arrOnClicks addObject:response]; | |
| 106 | + }]; | |
| 107 | +// [arrOnClicks addObject:clickString]; | |
| 108 | + } | |
| 109 | + return arrOnClicks; | |
| 110 | +} | |
| 111 | + | |
| 112 | +/// 改变背景颜色 | |
| 113 | +- (void)setBackgroundColor:(UIColor *)color { | |
| 114 | + NSString * jsString = [NSString stringWithFormat:@"document.body.style.backgroundColor = '%@'",[color webColorString]]; | |
| 115 | + [self evaluateJavaScript:jsString completionHandler:nil]; | |
| 116 | +} | |
| 117 | + | |
| 118 | +/// 为所有图片添加点击事件(网页中有些图片添加无效) | |
| 119 | +- (void)addClickEventOnImg { | |
| 120 | + int node = [self nodeCountOfTag:@"img"]; | |
| 121 | + for (int i = 0; i < node; i++) { | |
| 122 | + //利用重定向获取img.src,为区分,给url添加'img:'前缀 | |
| 123 | + NSString *jsString = [NSString stringWithFormat: | |
| 124 | + @"document.getElementsByTagName('img')[%d].onclick = \ | |
| 125 | + function() { document.location.href = 'img:' + this.src; }",i]; | |
| 126 | + [self evaluateJavaScript:jsString completionHandler:nil]; | |
| 127 | + } | |
| 128 | +} | |
| 129 | + | |
| 130 | +/// 改变指定标签的字体颜色 | |
| 131 | +- (void)setFontColor:(UIColor *)color withTag:(NSString *)tagName { | |
| 132 | + NSString *jsString = [NSString stringWithFormat: | |
| 133 | + @"var nodes = document.getElementsByTagName('%@'); \ | |
| 134 | + for(var i=0;i<nodes.length;i++){\ | |
| 135 | + nodes[i].style.color = '%@';}", tagName, [color webColorString]]; | |
| 136 | + [self evaluateJavaScript:jsString completionHandler:nil]; | |
| 137 | +} | |
| 138 | + | |
| 139 | +/// 改变指定标签的字体大小 | |
| 140 | +- (void)setFontSize:(int)size withTag:(NSString *)tagName { | |
| 141 | + NSString *jsString = [NSString stringWithFormat: | |
| 142 | + @"var nodes = document.getElementsByTagName('%@'); \ | |
| 143 | + for(var i=0;i<nodes.length;i++){\ | |
| 144 | + nodes[i].style.fontSize = '%dpx';}", tagName, size]; | |
| 145 | + [self evaluateJavaScript:jsString completionHandler:nil]; | |
| 146 | +} | |
| 147 | + | |
| 148 | +/// 设置网页没有拷贝粘贴 | |
| 149 | +- (void)disableTouchCallout { | |
| 150 | + [self evaluateJavaScript:@"document.documentElement.style.webkitUserSelect='none';" completionHandler:nil]; | |
| 151 | + [self evaluateJavaScript:@"document.documentElement.style.webkitTouchCallout='none';" completionHandler:nil]; | |
| 152 | +} | |
| 153 | + | |
| 154 | +@end | ... | ... |