JYNewWebController.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // JYNewWebController.m
  3. // JianYuIOS
  4. //
  5. // Created by apple on 2020/6/11.
  6. // Copyright © 2020 lixianglan. All rights reserved.
  7. //
  8. #import "JYNewWebController.h"
  9. #import <WebKit/WebKit.h>
  10. @interface JYNewWebController ()<WKNavigationDelegate,UIWebViewDelegate>
  11. @property (nonatomic, strong) UIProgressView *progressView;
  12. //@property (nonatomic, strong) WKWebView *webView;
  13. @property (nonatomic, strong) UIWebView *webView;
  14. @end
  15. @implementation JYNewWebController
  16. -(void)viewWillAppear:(BOOL)animated
  17. {
  18. [super viewWillAppear:animated];
  19. [self.navigationController setNavigationBarHidden:YES animated:animated];
  20. }
  21. //进度条
  22. - (UIProgressView *)progressView
  23. {
  24. if (_progressView == nil) {
  25. CGFloat H = 0.0;
  26. if (self.titleShow) {
  27. if ([self.titleShow isEqualToString:@"-1"]) {
  28. }else {
  29. H = NAVIGATION_BAR_HEIGHT;
  30. }
  31. }
  32. _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0,H - 2,WIDTH,2)];
  33. _progressView.tintColor = [UIColor blueColor];
  34. _progressView.trackTintColor = [UIColor whiteColor];
  35. }
  36. return _progressView;
  37. }
  38. #pragma mark - event response
  39. // 计算wkWebView进度条
  40. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  41. if (object == self.webView && [keyPath isEqualToString:@"estimatedProgress"]) {
  42. CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
  43. self.progressView.alpha = 1.0f;
  44. [self.progressView setProgress:newprogress animated:YES];
  45. if (newprogress >= 1.0f) {
  46. [UIView animateWithDuration:0.3f
  47. delay:0.3f
  48. options:UIViewAnimationOptionCurveEaseOut
  49. animations:^{
  50. self.progressView.alpha = 0.0f;
  51. }
  52. completion:^(BOOL finished) {
  53. [self.progressView setProgress:0 animated:NO];
  54. }];
  55. }
  56. } else {
  57. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  58. }
  59. }
  60. -(void)initUI {
  61. //默认都隐藏
  62. UIButton *backBtn_1 = [ZKControl createButtonWithFrame:CGRectMake(15, STATUS_BAR_HEIGHT+15, 30, 30) title:@"" imageName:@"back_normal" bgImageName:nil target:self method:@selector(backBtn1Click)];
  63. backBtn_1.backgroundColor = [UIColor blackColor];
  64. backBtn_1.layer.cornerRadius = 15;
  65. backBtn_1.layer.masksToBounds = YES;
  66. UIView *titleView = [ZKControl createViewWithFrame:CGRectMake(0, 0, WIDTH, NAVIGATION_BAR_HEIGHT) color:[UIColor whiteColor]];
  67. [self.view addSubview:titleView];
  68. //默认都隐藏
  69. UIButton *backBtn_2 = [ZKControl createButtonWithFrame:CGRectMake(10, STATUS_BAR_HEIGHT, 60, 44) title:@"" imageName:@"arrowLeft" bgImageName:nil target:self method:@selector(backBtn2Click)];
  70. UILabel *titleLabel = [ZKControl createLabelWithFrame:CGRectMake(50, STATUS_BAR_HEIGHT, WIDTH-100, 44) font:17 text:@"公告信息" TextAlignment:NSTextAlignmentCenter TextColor:UIColorFromRGB(0x333333) bgColor:nil];
  71. UIView *lineView= [ZKControl createViewWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT-1, WIDTH, 1) color:[UIColor groupTableViewBackgroundColor]];
  72. [titleView addSubview:titleLabel];
  73. [titleView addSubview:backBtn_2];
  74. [titleView addSubview:lineView];
  75. CGFloat H = 0.0;
  76. if (self.titleShow) {
  77. titleLabel.text = self.titleShow;
  78. if ([self.titleShow isEqualToString:@"-1"]) {
  79. titleView.hidden = YES;
  80. backBtn_1.hidden = YES;
  81. }else {
  82. titleView.hidden = NO;
  83. backBtn_1.hidden = YES;
  84. H = NAVIGATION_BAR_HEIGHT;
  85. }
  86. }else {
  87. titleView.hidden = YES;
  88. backBtn_1.hidden = NO;
  89. }
  90. self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, H, WIDTH, HEIGHT-STATUS_BAR_HEIGHT-H)];
  91. if (iPhoneX) {
  92. if (@available(iOS 11.0, *)) {
  93. self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  94. }
  95. }
  96. self.webView.scrollView.bounces = NO;
  97. _webView.scrollView.bounces = NO;
  98. _webView.delegate = self;
  99. _webView.userInteractionEnabled = YES;
  100. [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
  101. [self.view addSubview:self.webView];
  102. [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
  103. [self.view addSubview:backBtn_1];
  104. [self.view addSubview:titleView];
  105. }
  106. - (void)viewDidLoad {
  107. [super viewDidLoad];
  108. // Do any additional setup after loading the view.
  109. self.view.backgroundColor = [UIColor whiteColor];
  110. NSLog(@"title:%@",self.titleShow);
  111. NSLog(@"url:%@",self.url);
  112. [self initUI];//渲染页面
  113. [self.view addSubview:self.progressView];
  114. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissSelf) name:@"kNewWebControllerDismiss" object:nil];
  115. }
  116. #pragma mark - UIWebViewDelegate
  117. - (void)webViewDidStartLoad:(UIWebView *)webView {
  118. [[LXViewControllerManager shareViewControllerManager] showHudText:@""];
  119. }
  120. - (void)webViewDidFinishLoad:(UIWebView *)webView{
  121. [[LXViewControllerManager shareViewControllerManager] hideHud];
  122. [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
  123. // Disable callout
  124. [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
  125. }
  126. -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
  127. [[LXViewControllerManager shareViewControllerManager] hideHud];
  128. [[GlobalData shareInstance] addOnePlistData:@{
  129. @"detail":[[self.webView stringByEvaluatingJavaScriptFromString:@"document.location.href"] stringByAppendingString:[NSString stringWithFormat:@"\n%@",[error description]]],
  130. @"reqTime":[NSNumber numberWithInt:0],
  131. }];
  132. }
  133. #pragma mark - WKNavigationDelegate
  134. /* 页面加载完成 */
  135. //- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
  136. //
  137. // [self.webView evaluateJavaScript:@"document.documentElement.style.webkitTouchCallout='none';" completionHandler:nil];
  138. // [self.webView evaluateJavaScript:@"document.documentElement.style.webkitUserSelect='none';"completionHandler:nil];
  139. //}
  140. //// 页面开始加载时调用
  141. //- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
  142. //
  143. //}
  144. //// 当内容开始返回时调用
  145. //- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
  146. //}
  147. //
  148. //// 页面加载失败时调用
  149. //- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(nonnull NSError *)error{
  150. // [[GlobalData shareInstance] addOnePlistData:@{
  151. // @"detail":[[self stringByEvaluatingJavaScriptFromString:@"document.location.href"] stringByAppendingString:[NSString stringWithFormat:@"\n%@",[error description]]],
  152. // @"reqTime":[NSNumber numberWithInt:0],
  153. // }];
  154. //}
  155. //- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script
  156. //{
  157. // __block NSString *resultString = nil;
  158. // __block BOOL finished = NO;
  159. //
  160. // [self.webView evaluateJavaScript:script completionHandler:^(id result, NSError *error) {
  161. // if (error == nil) {
  162. // if (result != nil) {
  163. // resultString = [NSString stringWithFormat:@"%@", result];
  164. // }
  165. // } else {
  166. // NSLog(@"evaluateJavaScript error : %@", error.localizedDescription);
  167. // }
  168. // finished = YES;
  169. // }];
  170. //
  171. // while (!finished) {
  172. // [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
  173. // }
  174. // return resultString;
  175. //}
  176. -(void)backBtn1Click
  177. {
  178. if (self.webView.canGoBack) {// web能退 就后退
  179. [self.webView goBack];
  180. }else{// 跳回上级页面
  181. [self dismissViewControllerAnimated:YES completion:nil];
  182. }
  183. }
  184. -(void)backBtn2Click
  185. {
  186. if (self.webView.canGoBack) {// web能退 就后退
  187. [self.webView goBack];
  188. }else{// 跳回上级页面
  189. [self dismissViewControllerAnimated:YES completion:nil];
  190. }
  191. }
  192. -(void)dismissSelf{
  193. kMainQueue(^{
  194. [self dismissViewControllerAnimated:NO completion:nil];
  195. });
  196. }
  197. /*
  198. #pragma mark - Navigation
  199. // In a storyboard-based application, you will often want to do a little preparation before navigation
  200. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  201. // Get the new view controller using [segue destinationViewController].
  202. // Pass the selected object to the new view controller.
  203. }
  204. */
  205. @end