// // LDWebDetailController.m // YouHuiProject // // Created by 小花 on 2018/5/29. // Copyright © 2018年 kuxuan. All rights reserved. // #import "LDWebDetailController.h" #import #import "WYWeakScriptMessageDelegate.h" #import "LDFeedbackController.h" static NSString *app_h5_question = @"app_h5_question"; @interface LDWebDetailController () @property (nonatomic, strong) WKWebView *webView; /** 加载进度条 */ @property (nonatomic, strong) UIProgressView *progressView; @property (nonatomic, strong) UIButton *backButton; @property (nonatomic, strong) UIButton *closeButton; @end @implementation LDWebDetailController - (void)viewDidLoad { [super viewDidLoad]; [self configNavigationBar]; [self addUserhandler]; [self loadRequest]; } - (void)configNavigationBar { self.view.backgroundColor = [UIColor whiteColor]; [self.view addSubview:self.webView]; [self.view addSubview:self.progressView]; self.navigationBar.backgroundColor = [UIColor whiteColor]; self.navigationBar.showNavigationBarBottomLine = YES; [self.navigationBar setCustomLeftButtons:@[self.backButton]]; self.navigationBar.backgroundColor = [UIColor clearColor]; if (@available(iOS 11.0, *)) { self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用 }else { self.automaticallyAdjustsScrollViewInsets = NO; } } - (void)loadRequest { if (self.url.length > 0) { NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]; [self.webView loadRequest:request]; }else { [SVProgressHUD show]; NSString *url = [NSString stringWithFormat:@"%@/api/v2/homeact/guideBook",BaseURL]; [LDHttp get:url params:nil success:^(id json) { NSString *url; switch (self.pageType) { case 0: //新手指南 url = json[@"url"]; break; case 1: //赚钱秘籍 url = json[@"getMoneyUrl"]; break; case 2: //常见问题 url = json[@"commonProblems"]; break; default: break; } NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; [self.webView loadRequest:request]; [SVProgressHUD dismiss]; } failure:^(NSError *error) { [SVProgressHUD dismiss]; [MBProgressHUD showMessage:@"加载失败"]; }]; } } - (void)addUserhandler { [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_question]; } /** kvo监听进度条 @param keyPath keyPath description @param object object description @param change change description @param context context description */ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))] && object == self.webView ) { [self.progressView setAlpha:1.0]; [self.progressView setProgress:self.webView.estimatedProgress ]; if (self.webView.estimatedProgress >= 1.0) { [UIView animateWithDuration:0.7 animations:^{ [self.progressView setProgress:1.0 animated:YES]; [self.progressView setAlpha:0.0]; }]; } }else{ [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } } #pragma mark- #pragma mark- WKNavigationDelegate delegate /** 开始加载web的时候 @param webView webView description @param navigation navigation description */ - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{ //开始加载的时候,让进度条显示 self.progressView.hidden = NO; self.progressView.progress = 0; self.progressView.alpha = 1.0; [UIView animateWithDuration:0.8 animations:^{ self.progressView.progress = 0.6; }]; } /** 当网页加载完成的时候调用 @param webView web描述 @param navigation 导航的描述 */ - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{ [self.navigationBar setNavTitle:webView.title]; [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO]; [self updateNavigationBarButtons]; } #pragma mark --- WKScriptMessageHandler - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { if ([message.name isEqualToString:app_h5_question]) { LDFeedbackController *feedback = [[LDFeedbackController alloc] init]; [self.navigationController pushViewController:feedback animated:YES]; } } #pragma mark ----------- - (void)updateNavigationBarButtons { if (self.webView.canGoBack) { [self.navigationBar setCustomLeftButtons:@[self.backButton,self.closeButton]]; }else { [self.navigationBar setCustomLeftButtons:@[self.backButton]]; } } - (void)backAction { if (self.webView.canGoBack) { [self.webView goBack]; }else{ [self.navigationController popViewControllerAnimated:YES]; } } - (void)closeAction { [self.navigationController popViewControllerAnimated:YES]; } - (WKWebView *)webView{ if (!_webView) { _webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight)]; _webView.backgroundColor = [UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]; _webView.navigationDelegate = self; _webView.UIDelegate = self; //使用kvo监听进度 [_webView addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:0 context:nil]; //手势触摸滑动 _webView.allowsBackForwardNavigationGestures = YES; //自适应 [_webView sizeToFit]; //自适应 [_webView sizeToFit]; } return _webView; } #pragma mark- #pragma mark- SetupConstraints /** 加载配置以及视图添加*/ - (void)loadSubViewsConfiguration{ [self.view addSubview:self.webView]; } - (UIButton *)backButton { if (!_backButton) { _backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; [_backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal]; [_backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; } return _backButton; } - (UIButton *)closeButton { if (!_closeButton) { _closeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; [_closeButton setImage:[UIImage imageNamed:@"close_web"] forState:UIControlStateNormal]; [_closeButton addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside]; } return _closeButton; } - (UIProgressView *)progressView{ if (!_progressView) { _progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault]; _progressView.frame = CGRectMake(0, NavBarHeight, self.view.frame.size.width, 2); //设置进度条的色彩 [_progressView setTrackTintColor:[UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]]; _progressView.progressTintColor = [UIColor homeRedColor]; } return _progressView; } //观察的移除 - (void)dealloc{ [self.webView removeObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress))]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end