zoukankan      html  css  js  c++  java
  • iOS UI基础-21 WKWebView

    WKWebView,直接显示网页,需要引入第三方类:https://github.com/marcuswestin/WebViewJavascriptBridge

    加上进度条代码,很完美使用

    webView.h

    #import <UIKit/UIKit.h>
    
    @interface ECBankCardAddViewController : UIViewController
    /** 请求H5的时候,需要传的token */
    @property (nonatomic, copy) NSString *token;
    @end
    

     webView.m

    //
    //  ECBandCardAddViewController.m
    //  ECardBusiness
    //
    //  Created by yongsheng.jiang on 2018/2/6.
    //  Copyright © 2018年 bige. All rights reserved.
    //
    
    #import "ECBankCardAddViewController.h"
    #import "WebViewJavascriptBridge.h"
    
    @interface ECBankCardAddViewController ()<WKNavigationDelegate>
    @property (nonatomic, strong) WKWebView *webView;
    @property (nonatomic, weak) UIProgressView *progressView;
    
    @end
    
    @implementation ECBankCardAddViewController
    
    - (void)dealloc {
        [self.webView removeObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress))];
        self.webView.navigationDelegate = nil;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.title = @"添加银行卡";
        self.view.backgroundColor = [UIColor whiteColor];
        
        WKWebView *webView = [[NSClassFromString(@"WKWebView") alloc] initWithFrame:self.view.bounds];
        [self.view addSubview:webView];
        self.webView = webView;
        
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",web_add_bandcard,self.token]]];
        [webView loadRequest:request];
        
        [webView addObserver:self
                  forKeyPath:NSStringFromSelector(@selector(estimatedProgress))
                     options:0
                     context:nil];
        
        [self setupProgressView];
    }
    
    - (void)setupProgressView {
        UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
        progressView.frame = CGRectMake(0, 64, SCREEN_WIDTH, 5);
        
        [progressView setTrackTintColor:[UIColor colorWithRed:240.0/255
                                                        green:240.0/255
                                                         blue:240.0/255
                                                        alpha:1.0]];
        progressView.progressTintColor = [UIColor greenColor];
        [self.view addSubview:progressView];
        
        _progressView = progressView;
    }
    
    #pragma mark - WKNavigationDelegate
    - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
        NSLog(@"didStartProvisionalNavigation");
        //开始加载的时候,让进度条显示
        self.progressView.hidden = NO;
    }
    
    - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
        NSLog(@"didCommitNavigation");
    }
    
    - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
        NSLog(@"didFinishNavigation");
    }
    
    #pragma mark - KVO
    //kvo 监听进度
    -(void)observeValueForKeyPath:(NSString *)keyPath
                         ofObject:(id)object
                           change:(NSDictionary<NSKeyValueChangeKey,id> *)change
                          context:(void *)context{
        
        if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))]
            && object == self.webView) {
            [self.progressView setAlpha:1.0f];
            BOOL animated = self.webView.estimatedProgress > self.progressView.progress;
            [self.progressView setProgress:self.webView.estimatedProgress
                                  animated:animated];
            
            if (self.webView.estimatedProgress >= 1.0f) {
                [UIView animateWithDuration:0.3f
                                      delay:0.3f
                                    options:UIViewAnimationOptionCurveEaseOut
                                 animations:^{
                                     [self.progressView setAlpha:0.0f];
                                 }
                                 completion:^(BOOL finished) {
                                     [self.progressView setProgress:0.0f animated:NO];
                                 }];
            }
        } else{
            [super observeValueForKeyPath:keyPath
                                 ofObject:object
                                   change:change
                                  context:context];
        }
    }
    
    @end
    

    参考文档:http://blog.csdn.net/MyKingSaber/article/details/54134216 

  • 相关阅读:
    iOS设计模式
    Objective-C的可变是如何实现的?
    很好的UI动效设计参考
    MAC OS Finder 中快速定位指定路径
    IOS应用沙盒文件操作
    Java -- 单例模式
    Java眼中的XML--文件读取--2 应用SAX方式解析XML
    Java眼中的XML--文件读取--1 应用DOM方式解析XML
    配置JDK环境变量
    IOS开发之SVN的使用
  • 原文地址:https://www.cnblogs.com/jys509/p/8424141.html
Copyright © 2011-2022 走看看