TCWebViewController.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // SDKIntroViewController.m
  3. // TXXiaoShiPinDemo
  4. //
  5. // Created by shengcui on 2018/8/31.
  6. // Copyright © 2018年 tencent. All rights reserved.
  7. //
  8. #import "TCWebViewController.h"
  9. @interface TCWebViewController () <WKUIDelegate>
  10. {
  11. WKUserContentController *_contentController;
  12. NSURL *_url;
  13. }
  14. @end
  15. @implementation TCWebViewController
  16. - (instancetype)initWithURL:(NSString *)url {
  17. self = [super initWithNibName:nil bundle:nil];
  18. if (self) {
  19. _url = [NSURL URLWithString:url];
  20. }
  21. return self;
  22. }
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. // AppStore审核不允许出现'购买'和Android字样,去掉相关信息
  26. WKUserContentController *ucc = [[WKUserContentController alloc] init];
  27. _contentController = ucc;
  28. [self installUserScripts:ucc];
  29. WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
  30. config.userContentController = ucc;
  31. WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
  32. webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  33. webView.UIDelegate = self;
  34. [self.view addSubview:webView];
  35. webView.navigationDelegate = self;
  36. self.webView = webView;
  37. [webView loadRequest:[NSURLRequest requestWithURL:_url]];
  38. }
  39. - (void)installUserScripts:(WKUserContentController *)ucc {
  40. [ucc removeAllUserScripts];
  41. NSArray *jsCmdList = @[@"$(\"div.c-hero-section-btn>a.c-btn\").remove();"
  42. , @"$('div.sdk-wrap').remove();"
  43. , @"$('div.ugsv-buy').remove();"
  44. , @"$('li[data-type=\"pricing\"]').remove();"
  45. , @"$('video.ugsv-demo-video').removeAttr('autoplay');"
  46. , @"$('li[data-type=\"getting-started\"]').remove();"
  47. , @"$('a[href=\"https://cloud.tencent.com/document/product/584/9368\"]').remove();"
  48. , @"$('a[href=\"tel:4009-100-100\"]').text(function(){return $(this).text().replace('购买', '');});"];
  49. NSString *combine = [jsCmdList componentsJoinedByString:@""];
  50. NSString *observer = [NSString stringWithFormat: @"$('div.product-body.J-mainContent').bind('DOMSubtreeModified',function(e) {%@});", combine];
  51. NSString *final = [combine stringByAppendingString:observer];
  52. WKUserScript *script = [[WKUserScript alloc]initWithSource:final injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:NO];
  53. [ucc addUserScript:script];
  54. /*
  55. NSMutableArray *jsList = [NSMutableArray arrayWithCapacity:jsCmdList.count];
  56. for (NSString *js in jsCmdList) {
  57. [jsList addObject:[NSString stringWithFormat:@"$(document).ready(function() { %@; });", js]];
  58. }
  59. for (NSString *js in jsList) {
  60. WKUserScript *script = [[WKUserScript alloc]initWithSource:js injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:NO];
  61. [ucc addUserScript:script];
  62. }
  63. */
  64. }
  65. - (void)viewWillAppear:(BOOL)animated
  66. {
  67. [super viewWillAppear:animated];
  68. self.navigationController.navigationBarHidden = NO;
  69. }
  70. - (void)viewWillDisappear:(BOOL)animated
  71. {
  72. [super viewWillDisappear:animated];
  73. self.navigationController.navigationBarHidden = YES;
  74. }
  75. - (void)didReceiveMemoryWarning {
  76. [super didReceiveMemoryWarning];
  77. // Dispose of any resources that can be recreated.
  78. }
  79. - (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation;
  80. {
  81. self.title = webView.title;
  82. }
  83. - (BOOL)webView:(WKWebView *)webView shouldPreviewElement:(WKPreviewElementInfo *)elementInfo {
  84. return NO;
  85. }
  86. @end