123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- //
- // JYWKNewWebController.m
- // JianYuIOS
- //
- // Created by apple on 2020/8/24.
- // Copyright © 2020 lixianglan. All rights reserved.
- //
- #import "JYWKNewWebController.h"
- #import <WebKit/WebKit.h>
- @interface JYWKNewWebController ()<WKNavigationDelegate>
- @property (nonatomic, strong) UIProgressView *progressView;
- @property (nonatomic, strong) WKWebView *webView;
- @end
- @implementation JYWKNewWebController
- -(void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- [self.navigationController setNavigationBarHidden:YES animated:animated];
- }
- //进度条
- - (UIProgressView *)progressView
- {
- if (_progressView == nil) {
- CGFloat H = 0.0;
- if (self.titleShow ) {
- if ([self.titleShow isEqualToString:@"-1"]) {
-
- }else {
- H = NAVIGATION_BAR_HEIGHT;
- }
- }
- _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0,H - 2,WIDTH,2)];
- _progressView.tintColor = [UIColor blueColor];
- _progressView.trackTintColor = [UIColor whiteColor];
- }
- return _progressView;
- }
- #pragma mark - event response
- // 计算wkWebView进度条
- - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
- if (object == self.webView && [keyPath isEqualToString:@"estimatedProgress"]) {
- CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
- self.progressView.alpha = 1.0f;
- [self.progressView setProgress:newprogress animated:YES];
- if (newprogress >= 1.0f) {
- [UIView animateWithDuration:0.3f
- delay:0.3f
- options:UIViewAnimationOptionCurveEaseOut
- animations:^{
- self.progressView.alpha = 0.0f;
- }
- completion:^(BOOL finished) {
- [self.progressView setProgress:0 animated:NO];
- }];
- }
-
- } else {
- [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
- }
- }
- -(void)initUI {
- //默认都隐藏
- UIButton *backBtn_1 = [ZKControl createButtonWithFrame:CGRectMake(15, STATUS_BAR_HEIGHT+15, 30, 30) title:@"" imageName:@"back_normal" bgImageName:nil target:self method:@selector(backBtn1Click)];
- backBtn_1.backgroundColor = [UIColor blackColor];
- backBtn_1.layer.cornerRadius = 15;
- backBtn_1.layer.masksToBounds = YES;
-
-
- UIView *titleView = [ZKControl createViewWithFrame:CGRectMake(0, 0, WIDTH, NAVIGATION_BAR_HEIGHT) color:[UIColor whiteColor]];
- [self.view addSubview:titleView];
-
- //默认都隐藏
- UIButton *backBtn_2 = [ZKControl createButtonWithFrame:CGRectMake(10, STATUS_BAR_HEIGHT, 60, 44) title:@"" imageName:@"arrowLeft" bgImageName:nil target:self method:@selector(backBtn2Click)];
- UILabel *titleLabel = [ZKControl createLabelWithFrame:CGRectMake(50, STATUS_BAR_HEIGHT, WIDTH-100, 44) font:17 text:@"公告信息" TextAlignment:NSTextAlignmentCenter TextColor:UIColorFromRGB(0x333333) bgColor:nil];
- UIView *lineView= [ZKControl createViewWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT-1, WIDTH, 1) color:[UIColor groupTableViewBackgroundColor]];
-
- [titleView addSubview:titleLabel];
- [titleView addSubview:backBtn_2];
- [titleView addSubview:lineView];
- CGFloat H = 0.0;
- if (self.titleShow) {
- titleLabel.text = self.titleShow;
- if ([self.titleShow isEqualToString:@"-1"]) {
- titleView.hidden = YES;
- backBtn_1.hidden = YES;
- }else {
- titleView.hidden = NO;
- backBtn_1.hidden = YES;
- H = NAVIGATION_BAR_HEIGHT;
- }
- }else {
- titleView.hidden = YES;
- backBtn_1.hidden = NO;
- }
-
-
- WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
- config.preferences = [[WKPreferences alloc] init];
- config.preferences.minimumFontSize = 0;
- config.allowsInlineMediaPlayback = YES;
- self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, H, WIDTH, HEIGHT-STATUS_BAR_HEIGHT-H) configuration:config];
- if (iPhoneX) {
- if (@available(iOS 11.0, *)) {
- _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
- }
- }
- self.webView.scrollView.bounces = NO;
- self.webView.navigationDelegate = self;
- self.webView.userInteractionEnabled = YES;
- [self.view addSubview:self.webView];
- [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
- [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
- [self.view addSubview:backBtn_1];
- [self.view addSubview:titleView];
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
-
- self.view.backgroundColor = [UIColor whiteColor];
- NSLog(@"title:%@",self.titleShow);
- NSLog(@"url:%@",self.url);
- [self initUI];//渲染页面
- [self.view addSubview:self.progressView];
-
- }
- #pragma mark - WKNavigationDelegate
- /* 页面加载完成 */
- - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
-
- // NSLog(@"-----页面加载完成%@",webView.URL.absoluteString);
- [self.webView evaluateJavaScript:@"document.documentElement.style.webkitTouchCallout='none';" completionHandler:nil];
- }
- // 页面开始加载时调用
- - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
- }
- // 当内容开始返回时调用
- - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
- }
- // 页面加载失败时调用
- - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(nonnull NSError *)error{
- [[GlobalData shareInstance] addOnePlistData:@{
- @"detail":[[self stringByEvaluatingJavaScriptFromString:@"document.location.href"] stringByAppendingString:[NSString stringWithFormat:@"\n%@",[error description]]],
- @"reqTime":[NSNumber numberWithInt:0],
- }];
- }
- -(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
- {
- //如果是跳转一个新页面
- if (navigationAction.targetFrame == nil) {
- [webView loadRequest:navigationAction.request];
- }
-
- decisionHandler(WKNavigationActionPolicyAllow);
- }
- - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script
- {
- __block NSString *resultString = nil;
- __block BOOL finished = NO;
- [self.webView evaluateJavaScript:script completionHandler:^(id result, NSError *error) {
- if (error == nil) {
- if (result != nil) {
- resultString = [NSString stringWithFormat:@"%@", result];
- }
- } else {
- NSLog(@"evaluateJavaScript error : %@", error.localizedDescription);
- }
- finished = YES;
- }];
- while (!finished) {
- [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
- }
- return resultString;
- }
- -(void)backBtn1Click
- {
- if (self.webView.canGoBack) {// web能退 就后退
- [self.webView goBack];
- }else{// 跳回上级页面
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- }
- -(void)backBtn2Click
- {
- if (self.webView.canGoBack) {// web能退 就后退
- [self.webView goBack];
- }else{// 跳回上级页面
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- }
- -(void)dismissSelf{
- kMainQueue(^{
- [self dismissViewControllerAnimated:NO completion:nil];
- });
- }
- -(void)dealloc {
- NSLog(@"释放");
- }
- /*
- #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
|