猎豆优选

LDWebDetailController.m 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //
  2. // LDWebDetailController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/5/29.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "LDWebDetailController.h"
  9. #import <WebKit/WebKit.h>
  10. #import "WYWeakScriptMessageDelegate.h"
  11. #import "LDFeedbackController.h"
  12. static NSString *app_h5_question = @"app_h5_question";
  13. @interface LDWebDetailController ()<WKUIDelegate, WKNavigationDelegate,WKScriptMessageHandler>
  14. @property (nonatomic, strong) WKWebView *webView;
  15. /** 加载进度条 */
  16. @property (nonatomic, strong) UIProgressView *progressView;
  17. @property (nonatomic, strong) UIButton *backButton;
  18. @property (nonatomic, strong) UIButton *closeButton;
  19. @end
  20. @implementation LDWebDetailController
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. [self configNavigationBar];
  24. [self addUserhandler];
  25. [self loadRequest];
  26. }
  27. - (void)configNavigationBar {
  28. self.view.backgroundColor = [UIColor whiteColor];
  29. [self.view addSubview:self.webView];
  30. [self.view addSubview:self.progressView];
  31. self.navigationBar.backgroundColor = [UIColor whiteColor];
  32. self.navigationBar.showNavigationBarBottomLine = YES;
  33. [self.navigationBar setCustomLeftButtons:@[self.backButton]];
  34. self.navigationBar.backgroundColor = [UIColor clearColor];
  35. if (@available(iOS 11.0, *)) {
  36. self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
  37. }else {
  38. self.automaticallyAdjustsScrollViewInsets = NO;
  39. }
  40. }
  41. - (void)loadRequest {
  42. if (self.url.length > 0) {
  43. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
  44. [self.webView loadRequest:request];
  45. }else {
  46. [SVProgressHUD show];
  47. NSString *url = [NSString stringWithFormat:@"%@/api/v2/homeact/guideBook",BaseURL];
  48. [LDHttp get:url params:nil success:^(id json) {
  49. NSString *url;
  50. switch (self.pageType) {
  51. case 0:
  52. //新手指南
  53. url = json[@"url"];
  54. break;
  55. case 1:
  56. //赚钱秘籍
  57. url = json[@"getMoneyUrl"];
  58. break;
  59. case 2:
  60. //常见问题
  61. url = json[@"commonProblems"];
  62. break;
  63. default:
  64. break;
  65. }
  66. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
  67. [self.webView loadRequest:request];
  68. [SVProgressHUD dismiss];
  69. } failure:^(NSError *error) {
  70. [SVProgressHUD dismiss];
  71. [MBProgressHUD showMessage:@"加载失败"];
  72. }];
  73. }
  74. }
  75. - (void)addUserhandler {
  76. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_question];
  77. }
  78. /**
  79. kvo监听进度条
  80. @param keyPath keyPath description
  81. @param object object description
  82. @param change change description
  83. @param context context description
  84. */
  85. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
  86. if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))] && object == self.webView ) {
  87. [self.progressView setAlpha:1.0];
  88. [self.progressView setProgress:self.webView.estimatedProgress ];
  89. if (self.webView.estimatedProgress >= 1.0) {
  90. [UIView animateWithDuration:0.7 animations:^{
  91. [self.progressView setProgress:1.0 animated:YES];
  92. [self.progressView setAlpha:0.0];
  93. }];
  94. }
  95. }else{
  96. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  97. }
  98. }
  99. #pragma mark-
  100. #pragma mark- WKNavigationDelegate delegate
  101. /**
  102. 开始加载web的时候
  103. @param webView webView description
  104. @param navigation navigation description
  105. */
  106. - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
  107. //开始加载的时候,让进度条显示
  108. self.progressView.hidden = NO;
  109. self.progressView.progress = 0;
  110. self.progressView.alpha = 1.0;
  111. [UIView animateWithDuration:0.8 animations:^{
  112. self.progressView.progress = 0.6;
  113. }];
  114. }
  115. /**
  116. 当网页加载完成的时候调用
  117. @param webView web描述
  118. @param navigation 导航的描述
  119. */
  120. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
  121. [self.navigationBar setNavTitle:webView.title];
  122. [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
  123. [self updateNavigationBarButtons];
  124. }
  125. #pragma mark --- WKScriptMessageHandler
  126. - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
  127. if ([message.name isEqualToString:app_h5_question]) {
  128. LDFeedbackController *feedback = [[LDFeedbackController alloc] init];
  129. [self.navigationController pushViewController:feedback animated:YES];
  130. }
  131. }
  132. #pragma mark -----------
  133. - (void)updateNavigationBarButtons {
  134. if (self.webView.canGoBack) {
  135. [self.navigationBar setCustomLeftButtons:@[self.backButton,self.closeButton]];
  136. }else {
  137. [self.navigationBar setCustomLeftButtons:@[self.backButton]];
  138. }
  139. }
  140. - (void)backAction {
  141. if (self.webView.canGoBack) {
  142. [self.webView goBack];
  143. }else{
  144. [self.navigationController popViewControllerAnimated:YES];
  145. }
  146. }
  147. - (void)closeAction {
  148. [self.navigationController popViewControllerAnimated:YES];
  149. }
  150. - (WKWebView *)webView{
  151. if (!_webView) {
  152. _webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight)];
  153. _webView.backgroundColor = [UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0];
  154. _webView.navigationDelegate = self;
  155. _webView.UIDelegate = self;
  156. //使用kvo监听进度
  157. [_webView addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:0 context:nil];
  158. //手势触摸滑动
  159. _webView.allowsBackForwardNavigationGestures = YES;
  160. //自适应
  161. [_webView sizeToFit];
  162. //自适应
  163. [_webView sizeToFit];
  164. }
  165. return _webView;
  166. }
  167. #pragma mark-
  168. #pragma mark- SetupConstraints
  169. /** 加载配置以及视图添加*/
  170. - (void)loadSubViewsConfiguration{
  171. [self.view addSubview:self.webView];
  172. }
  173. - (UIButton *)backButton {
  174. if (!_backButton) {
  175. _backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  176. [_backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
  177. [_backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  178. }
  179. return _backButton;
  180. }
  181. - (UIButton *)closeButton {
  182. if (!_closeButton) {
  183. _closeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  184. [_closeButton setImage:[UIImage imageNamed:@"close_web"] forState:UIControlStateNormal];
  185. [_closeButton addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
  186. }
  187. return _closeButton;
  188. }
  189. - (UIProgressView *)progressView{
  190. if (!_progressView) {
  191. _progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
  192. _progressView.frame = CGRectMake(0, NavBarHeight, self.view.frame.size.width, 2);
  193. //设置进度条的色彩
  194. [_progressView setTrackTintColor:[UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]];
  195. _progressView.progressTintColor = [UIColor homeRedColor];
  196. }
  197. return _progressView;
  198. }
  199. //观察的移除
  200. - (void)dealloc{
  201. [self.webView removeObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress))];
  202. }
  203. - (void)didReceiveMemoryWarning {
  204. [super didReceiveMemoryWarning];
  205. // Dispose of any resources that can be recreated.
  206. }
  207. /*
  208. #pragma mark - Navigation
  209. // In a storyboard-based application, you will often want to do a little preparation before navigation
  210. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  211. // Get the new view controller using [segue destinationViewController].
  212. // Pass the selected object to the new view controller.
  213. }
  214. */
  215. @end