From 3e8437ae559487362fae3525beb79c534c213a51 Mon Sep 17 00:00:00 2001 From: 单军华 Date: Thu, 12 Jul 2018 13:44:34 +0800 Subject: [PATCH] bug修复和功能优化 --- screendisplay/screendisplay/Classes/Base/BaseControllers/LMJWebViewController.m | 404 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 404 insertions(+), 0 deletions(-) diff --git a/screendisplay/screendisplay/Classes/Base/BaseControllers/LMJWebViewController.m b/screendisplay/screendisplay/Classes/Base/BaseControllers/LMJWebViewController.m new file mode 100755 index 0000000..852feb6 --- /dev/null +++ b/screendisplay/screendisplay/Classes/Base/BaseControllers/LMJWebViewController.m @@ -0,0 +1,404 @@ +// +// LMJWebViewController.m +// PLMMPRJK +// +// Created by NJHu on 2017/4/9. +// Copyright �� 2017��� GoMePrjk. All rights reserved. +// + +#import "LMJWebViewController.h" +@interface LMJWebViewController () + +/** <#digest#> */ +@property (weak, nonatomic) UIProgressView *progressView; + +/** <#digest#> */ +@property (strong, nonatomic) UIButton *backBtn; + +/** <#digest#> */ +@property (strong, nonatomic) UIButton *closeBtn; + +@end + +@implementation LMJWebViewController + +- (void)setGotoURL:(NSString *)gotoURL { +// @"`#%^{}\"[]|\\<> " ��������������������� + _gotoURL = [gotoURL stringByAddingPercentEncodingWithAllowedCharacters:[[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "] invertedSet]]; +} + +#pragma mark - ������������ +- (void)viewDidLoad { + + [super viewDidLoad]; + + self.fd_interactivePopDisabled = YES; + + TWWeak(self); + [self.webView addObserverBlockForKeyPath:TWKeyPath(weakself.webView, estimatedProgress) block:^(id _Nonnull obj, id _Nullable oldVal, id _Nullable newVal) { + + weakself.progressView.progress = weakself.webView.estimatedProgress; + // ������������ + if (weakself.webView.estimatedProgress >= 1.0f ) { + + [UIView animateWithDuration:0.25f animations:^{ + weakself.progressView.alpha = 0.0f; + weakself.progressView.progress = 0.0f; + }]; + + }else{ + weakself.progressView.alpha = 1.0f; + } + + }]; + + [self.webView addObserverBlockForKeyPath:TWKeyPath(self.webView, title) block:^(id _Nonnull obj, id _Nullable oldVal, id _Nullable newVal) { + + if (!TWIsEmpty(newVal) && [newVal isKindOfClass:[NSString class]] && [self webViewController:self webViewIsNeedAutoTitle:self.webView]) { + weakself.title = newVal; + } + + }]; + + + [self.webView.scrollView addObserverBlockForKeyPath:TWKeyPath(self.webView.scrollView, contentSize) block:^(id _Nonnull obj, id _Nullable oldVal, id _Nullable newVal) { + [weakself webView:weakself.webView scrollView:weakself.webView.scrollView contentSize:weakself.webView.scrollView.contentSize]; + }]; + + if (self.gotoURL.length > 0) { + [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.gotoURL]]]; + }else if (!TWIsEmpty(self.contentHTML)) { + [self.webView loadHTMLString:self.contentHTML baseURL:nil]; + } +} + + + +- (void)viewDidLayoutSubviews +{ + [super viewDidLayoutSubviews]; + [self.view bringSubviewToFront:self.progressView]; +} + + +#pragma mark - LMJNavUIBaseViewControllerDataSource +#pragma mark - ��������������������������������������������������������� +/** ��������������������� view */ +- (UIView *)lmjNavigationBarLeftView:(LMJNavigationBar *)navigationBar +{ + UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 90, 44)]; + + leftView.backgroundColor = [UIColor yellowColor]; + + self.backBtn.mj_origin = CGPointZero; + + self.closeBtn.lmj_x = leftView.lmj_width - self.closeBtn.lmj_width; + + [leftView addSubview:self.backBtn]; + + [leftView addSubview:self.closeBtn]; + + return leftView; +} + + + +- (void)leftButtonEvent:(UIButton *)sender navigationBar:(LMJNavigationBar *)navigationBar +{ + [self backBtnClick:sender webView:self.webView]; +} + +- (void)left_close_button_event:(UIButton *)sender +{ + [self closeBtnClick:sender webView:self.webView]; +} + + + + +#pragma mark - LMJWebViewControllerDataSource +// ������������, ��������������������� +- (BOOL)webViewController:(LMJWebViewController *)webViewController webViewIsNeedProgressIndicator:(WKWebView *)webView +{ + return YES; +} + +// ������������������������������ +- (BOOL)webViewController:(LMJWebViewController *)webViewController webViewIsNeedAutoTitle:(WKWebView *)webView +{ + return YES; +} + + +#pragma mark - LMJWebViewControllerDelegate +// ��������������������������������������� +- (void)backBtnClick:(UIButton *)backBtn webView:(WKWebView *)webView +{ + if (self.webView.canGoBack) { + self.closeBtn.hidden = NO; + [self.webView goBack]; + }else + { + [self closeBtnClick:self.closeBtn webView:self.webView]; + } +} + +// ��������������������� +- (void)closeBtnClick:(UIButton *)closeBtn webView:(WKWebView *)webView { + // ������������������: push ��� present + if ((self.navigationController.presentedViewController || self.navigationController.presentingViewController) && self.navigationController.childViewControllers.count == 1) { + [self dismissViewControllerAnimated:YES completion:nil]; + }else{ + [self.navigationController popViewControllerAnimated:YES]; + } +} + +// ������ self.webView.scrollView ��� contentSize ������������������������������������������������ View ������������������ +- (void)webView:(WKWebView *)webView scrollView:(UIScrollView *)scrollView contentSize:(CGSize)contentSize +{ + NSLog(@"%@\n%@\n%@", webView, scrollView, NSStringFromCGSize(contentSize)); +} + +#pragma mark - webDelegate + +// 1, ������������������������������������������ +- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { + + NSLog(@"decidePolicyForNavigationAction ==== %@", navigationAction); + decisionHandler(WKNavigationActionPolicyAllow); + +} + +// 2��������������������� +- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation { + + NSLog(@"didStartProvisionalNavigation ==== %@", navigation); + +} + + +// 4, ��������������������������������������� +- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler { + + NSLog(@"decidePolicyForNavigationResponse ==== %@", navigationResponse); + decisionHandler(WKNavigationResponsePolicyAllow); +} + +// 5,��������������������������� +- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation { + + NSLog(@"didCommitNavigation ==== %@", navigation); +} + +// 3, 6, ������ HTTPS ��������������������������������������� \ ������ HTTPS ��������������������������������������������������������� +- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler { + + NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; + completionHandler(NSURLSessionAuthChallengeUseCredential, credential); + +// if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { +// if ([challenge previousFailureCount] == 0) { +// +// NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; +// completionHandler(NSURLSessionAuthChallengeUseCredential, credential); +// } else { +// +// completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil); +// } +// } else { +// +// completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil); +// } +} + +// 7��������������������� +- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation { + NSLog(@"didFinishNavigation ==== %@", navigation); + +} + +// 8��������������������������� +- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error { + NSLog(@"didFailProvisionalNavigation ==== %@\nerror ==== %@", navigation, error); + [MBProgressHUD showError:@"������������������" ToView:self.view]; +} + +//��� WKWebView ������������������������������������������������������������������������������������ +- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView { + [webView reload]; + NSLog(@"webViewWebContentProcessDidTerminate"); +} + +#pragma mark - ��������� + +- (WKWebView *)webView +{ + if(_webView == nil) + { + //���������������WKWebViewConfiguration������ + WKWebViewConfiguration *config = [WKWebViewConfiguration new]; + //������������������������������preferences + config.preferences = [WKPreferences new]; + //The minimum font size in points default is 0; + config.preferences.minimumFontSize = 0; + //������������JavaScript + config.preferences.javaScriptEnabled = YES; + //������������������������������������������������ + config.preferences.javaScriptCanOpenWindowsAutomatically = YES; + // ������������������������������������������������������ + config.dataDetectorTypes = UIDataDetectorTypeAll; + // ������������ + config.allowsInlineMediaPlayback = YES; + + // ������������������ +// TWWeak(self); +// config.userContentController = [[WKUserContentController alloc] init]; +// [config.userContentController addScriptMessageHandler:weakself name:<#(nonnull NSString *)#>]; + + WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config]; + [self.view addSubview:webView]; + _webView = webView; + webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; + + webView.navigationDelegate = self; + webView.UIDelegate = self; + + webView.opaque = NO; + webView.backgroundColor = [UIColor clearColor]; + + //��������������������� + webView.allowsBackForwardNavigationGestures = YES; + + if ([self.parentViewController isKindOfClass:[UINavigationController class]]) { + UIEdgeInsets contentInset = webView.scrollView.contentInset; + contentInset.top += self.lmj_navgationBar.lmj_height; + webView.scrollView.contentInset = contentInset; + if (@available(iOS 11.0, *)){ + webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; + } + webView.scrollView.scrollIndicatorInsets = webView.scrollView.contentInset; + } + } + return _webView; +} + + +- (UIProgressView *)progressView +{ + if(_progressView == nil && [self.parentViewController isKindOfClass:[UINavigationController class]]) + { + UIProgressView *progressView = [[UIProgressView alloc] init]; + + [self.view addSubview:progressView]; + + _progressView = progressView; + + progressView.lmj_height = 1; + + progressView.lmj_width = kScreenWidth; + + progressView.lmj_y = self.lmj_navgationBar.lmj_height; + progressView.tintColor = [UIColor greenColor]; + + if ([self respondsToSelector:@selector(webViewController:webViewIsNeedProgressIndicator:)]) { + if (![self webViewController:self webViewIsNeedProgressIndicator:self.webView]) { + progressView.hidden = YES; + } + } + + } + return _progressView; +} + + +- (UIButton *)backBtn +{ + if(_backBtn == nil) + { + UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; + [btn setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal]; + + [btn setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted]; + + btn.lmj_size = CGSizeMake(34, 44); + + [btn addTarget:self action:@selector(leftButtonEvent:navigationBar:) forControlEvents:UIControlEventTouchUpInside]; + + _backBtn = btn; + } + return _backBtn; +} + +- (UIButton *)closeBtn +{ + if(_closeBtn == nil) + { + UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; + + [btn setTitle:@"������" forState:UIControlStateNormal]; + + [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; + + [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted]; + + btn.lmj_size = CGSizeMake(44, 44); + + btn.hidden = YES; + + [btn addTarget:self action:@selector(left_close_button_event:) forControlEvents:UIControlEventTouchUpInside]; + + _closeBtn = btn; + } + return _closeBtn; +} + +- (void)dealloc +{ + NSLog(@"LMJWebViewController -- dealloc"); + + [_webView.scrollView removeObserverBlocks]; + [_webView removeObserverBlocks]; + + _webView.UIDelegate = nil; + _webView.navigationDelegate = nil; + _webView.scrollView.delegate = nil; +} + + +@end + + + +// UIWebView ������������������������������ +//- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { +// if ([navigationAction.request.URL.absoluteString containsString:@"https://"] && IOSVersion < 9.0 && !self.httpsAuth) { +// self.originRequest = navigationAction.request; +// self.httpsUrlConnection = [[NSURLConnection alloc] initWithRequest:self.originRequest delegate:self]; +// [self.httpsUrlConnection start]; +// decisionHandler(WKNavigationActionPolicyCancel); +// return; +// } +// decisionHandler(WKNavigationActionPolicyAllow); +//} +// +//- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { +// if ([challenge previousFailureCount] == 0) { +// self.httpsAuth = YES; +// NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; +// [challenge.sender useCredential:credential forAuthenticationChallenge:challenge]; +// } else { +// [[challenge sender] cancelAuthenticationChallenge:challenge]; +// } +//} +// +//- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { +// self.httpsAuth = YES; +// [self.webView loadRequest:self.originRequest]; +// [self.httpsUrlConnection cancel]; +//} +// +//- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { +// return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; +//} + + -- Gitblit v1.8.0