FaDetaiH5ViewController.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // FaDetaiH5ViewController.m
  3. // jitao
  4. //
  5. // Created by 罗云飞 on 2018/9/7.
  6. // Copyright © 2018年 罗云飞. All rights reserved.
  7. //
  8. #import "FaDetaiH5ViewController.h"
  9. #import "TGWebProgressLayer.h"
  10. #import <WebKit/WebKit.h>
  11. #import "NewKeFuViewController.h"
  12. @interface FaDetaiH5ViewController ()<WKNavigationDelegate,UIWebViewDelegate>{
  13. UIWebView *_myWebView;
  14. }
  15. @property (nonatomic, strong)WKWebView *mainWebView;
  16. @property (nonatomic, strong)TGWebProgressLayer *webProgressLayer;
  17. @property (strong ,nonatomic) UIButton * registerAccount;
  18. @end
  19. @implementation FaDetaiH5ViewController
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. [self setNavTitle:[NSString stringWithFormat:@"%@详情",self.type]];
  23. NSString *ind;
  24. if ([self.type isEqualToString:@"政策"]) {
  25. ind = @"0";
  26. }else{
  27. ind = @"1";
  28. }
  29. NSString *htmlPath = [NSString stringWithFormat:@"%@/1.0/template/find/findDetails.html?id=%@&&ind=%@",HTML5_URL,self.ID,ind];
  30. _myWebView = [[UIWebView alloc] init];
  31. // [_myWebView.scrollView setDelegate:self];
  32. [_myWebView setDelegate:self];
  33. _myWebView.scrollView.tag = 112;
  34. _myWebView.backgroundColor = [UIColor clearColor];
  35. //自动对页面进行缩放以适应屏幕
  36. [_myWebView setScalesPageToFit:YES];
  37. if (htmlPath)
  38. {
  39. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:htmlPath] cachePolicy:NSURLRequestReloadRevalidatingCacheData timeoutInterval:60*60*24*1];
  40. [_myWebView loadRequest:request];
  41. }
  42. [self.view addSubview:_myWebView];
  43. // NSURL *baseURL = [NSURL fileURLWithPath:htmlPath];
  44. // [webView loadHTMLString:htmlPath baseURL:baseURL];
  45. _myWebView.sd_layout
  46. .rightSpaceToView(self.view,0)
  47. .topSpaceToView(self.view,NavHeader)
  48. .heightIs(SCREEN_HEIGHT-NavHeader)
  49. .widthIs(SCREEN_WIDTH);
  50. // [self loadsView];
  51. // Do any additional setup after loading the view.
  52. }
  53. - (UIButton *)registerAccount
  54. {
  55. if (!_registerAccount)
  56. {
  57. _registerAccount = [UIButton buttonWithType:UIButtonTypeCustom];
  58. // [_registerAccount setTitle:@"" forState:UIControlStateNormal];
  59. [_registerAccount setTitleColor:[UIColor colorWithString:@"#9A9A9A"] forState:UIControlStateNormal];
  60. [_registerAccount setBackgroundColor:[UIColor clearColor]];
  61. [_registerAccount setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
  62. [_registerAccount.titleLabel setFont:[UIFont systemFontOfSize:fitScreenWidth(14)]];
  63. [_registerAccount addTarget:self action:@selector(touchRegister:) forControlEvents:UIControlEventTouchUpInside];
  64. // [_registerAccount setupAutoSizeWithHorizontalPadding:5 buttonHeight:44];
  65. [_registerAccount setImage:NewImageNamed(@"gd") forState:UIControlStateNormal];
  66. }
  67. return _registerAccount;
  68. }
  69. #pragma mark - UI初始化
  70. - (void)loadsView
  71. {
  72. // [self.navgationBar addSubview:self.registerAccount];
  73. // _registerAccount.sd_layout
  74. // .widthIs(30)
  75. // .rightSpaceToView(self.navgationBar, 10)
  76. // .heightIs(44)
  77. // .topSpaceToView(self.navgationBar, 18);
  78. self.mainWebView = [WKWebView new];
  79. self.mainWebView.navigationDelegate =self;
  80. [self.view addSubview:self.mainWebView];
  81. self.mainWebView.sd_layout
  82. .rightSpaceToView(self.view,0)
  83. .topSpaceToView(self.view,NavHeader)
  84. .heightIs(SCREEN_HEIGHT-NavHeader)
  85. .widthIs(SCREEN_WIDTH);
  86. [self loadContent];
  87. self.webProgressLayer = [[TGWebProgressLayer alloc] init];
  88. self.webProgressLayer.frame = CGRectMake(0, NavHeader-2, WIDTH, 2);
  89. self.webProgressLayer.strokeColor = NewRGBColor(30, 144, 255, 1).CGColor;
  90. [self.view.layer addSublayer:self.webProgressLayer];
  91. UIView *view = [UIView new];
  92. view.backgroundColor = NewWhiteColor;
  93. // [self.view addSubview:view];
  94. view.sd_layout
  95. .leftEqualToView(self.view)
  96. .widthIs(SCREEN_WIDTH)
  97. .heightIs(45)
  98. .bottomEqualToView(self.view);
  99. UIButton *button = [UIButton new];
  100. [button setTitle:@"立即咨询" forState:UIControlStateNormal];
  101. [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
  102. button.titleLabel.font = NewFont(16);
  103. ViewBorderRadius(button, 5, 1, NewLineGrayColor);
  104. NewTouchUpInside(button, buttonclick:);
  105. [view addSubview:button];
  106. button.sd_layout
  107. .leftSpaceToView(view, 15)
  108. .rightSpaceToView(view, 15)
  109. .heightIs(35)
  110. .topSpaceToView(view, 5);
  111. }
  112. - (void)buttonclick:(UIButton *)sender {
  113. NewKeFuViewController *vc = [NewKeFuViewController new];
  114. NewPushViewController(vc);
  115. }
  116. #pragma mark - 加载Web数据
  117. -(void)loadContent
  118. {
  119. // int x = arc4random() % 10000000;
  120. // http://muat.jishutao.com/1.0/template/find/findDetails.html?id=243763876884570112&&ind=0
  121. // http://muat.jishutao.com/1.0/template/project/projectDetail.html?id=308087e5-b1d0-48fa-b1d1-d9b86b9062be
  122. NSString *ind;
  123. if ([self.type isEqualToString:@"政策"]) {
  124. ind = @"0";
  125. }else{
  126. ind = @"1";
  127. }
  128. NSString *url = [NSString stringWithFormat:@"%@/1.0/template/find/findDetails.html?id=%@&&ind=%@",HTML5_URL,self.ID,ind];
  129. NSURL *urlString = [NSURL URLWithString:url];
  130. // NSURLRequest *request = [NSURLRequest requestWithURL:urlString];
  131. //加载请求的时候忽略缓存
  132. NSURLRequest *request = [NSURLRequest requestWithURL:urlString cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5.0];
  133. [self.mainWebView loadRequest:request];
  134. }
  135. #pragma mark - WKWebViewDelegate
  136. - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
  137. [self.webProgressLayer tg_startLoad];
  138. }
  139. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
  140. [self.webProgressLayer tg_finishedLoadWithError:nil];
  141. }
  142. - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
  143. [self.webProgressLayer tg_finishedLoadWithError:error];
  144. }
  145. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
  146. NSString *hostname = navigationAction.request.URL.host.lowercaseString;
  147. if (navigationAction.navigationType == WKNavigationTypeLinkActivated && ![hostname containsString:HTML5_URL]) {
  148. decisionHandler(WKNavigationActionPolicyCancel);
  149. }else {
  150. decisionHandler(WKNavigationActionPolicyAllow);
  151. }
  152. }
  153. //UIWebView的代理方法
  154. - (void)webViewDidFinishLoad:(UIWebView *)webView
  155. {
  156. //具体去除广告的操作
  157. //查看更详细的操作进入:http://jwdev.cn/2015/09/28/use-javascript-to-delete-web-element/
  158. [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('m-container')[0].style.display = 'none'"];
  159. }
  160. - (void)didReceiveMemoryWarning {
  161. [super didReceiveMemoryWarning];
  162. // Dispose of any resources that can be recreated.
  163. }
  164. /*
  165. #pragma mark - Navigation
  166. // In a storyboard-based application, you will often want to do a little preparation before navigation
  167. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  168. // Get the new view controller using [segue destinationViewController].
  169. // Pass the selected object to the new view controller.
  170. }
  171. */
  172. @end