JYWKNewWebController.m 8.1 KB

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