WKWebView基本使用

直接代码
.h文件

#import 
@interface HWGameDetailsWebViewController : UIViewController
/** <#注释#> */
@property(nonatomic, strong) NSURL *url;
@end

.m文件

#import "HWGameDetailsWebViewController.h"
#import 

@interface HWGameDetailsWebViewController ()
/** <#注释#> */
@property(nonatomic, strong) WKWebView *webView;
/** <#注释#> */
@property(nonatomic, strong) UIView *mainView;
/** <#注释#> */
@property(nonatomic, strong) UIProgressView *progressView;
@end

@implementation HWGameDetailsWebViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addMainView];
    [self creatWebView];
}

#pragma mark - 添加进度条
- (void)addMainView {
    HWWeakSelf(weakSelf)
    UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, HWScreenW, HWScreenH)];
    mainView.backgroundColor = [UIColor clearColor];
    _mainView = mainView;
    [weakSelf.view addSubview:mainView];
    UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 64, HWScreenW, 1)];
    progressView.progress = 0;
    _progressView = progressView;
    [weakSelf.view addSubview:progressView];
}
- (void)creatWebView {
    HWWeakSelf(weakSelf)
    _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 64, HWScreenW, HWScreenH - 64)];
    _webView.backgroundColor = [UIColor whiteColor];
      NSURLRequest *request = [NSURLRequest requestWithURL:weakSelf.url];
//     https://www.baidu.com
//    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]];
    _webView.navigationDelegate = weakSelf;
    _webView.scrollView.bounces = NO;
    //  [_webView loadRequest:request];
    [_webView loadRequest:request];
    [weakSelf.mainView addSubview:_webView];
    
    // 添加观察者
    [_webView addObserver:weakSelf forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL]; // 进度
    [_webView addObserver:weakSelf forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL]; // 标题
}
#pragma mark - WKNavigationDelegate
#pragma mark - 截取当前加载的URL
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    HWWeakSelf(weakSelf)
    NSURL *URL = navigationAction.request.URL;
    HWLog(@"%@", URL); // 当前加载的URL
// 判断正在加载的URL与主页URL是否相等
    if (![[NSString stringWithFormat:@"%@", weakSelf.url] isEqualToString:[NSString stringWithFormat:@"%@", URL]]) { // 不相等 跳转控制器
        HWStrategyDetailsViewController *vc = [[HWStrategyDetailsViewController alloc] init];
        vc.url = URL;
        [weakSelf.navigationController pushViewController:vc animated:YES];
        decisionHandler(WKNavigationActionPolicyCancel); // 取消当前加载 // 注意:必须实现 不然会崩溃 
    }else { // 相等就加载当前的URL
        weakSelf.navigationView.titleLabel.text = weakSelf.title;
        decisionHandler(WKNavigationActionPolicyAllow);  // 继续加载 // 必须实现 不然会崩溃
    }
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
//    self.noDataView.alpha = 1;
}
// 页面加载完毕时调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
//    self.noDataView.alpha = 0;
}
#pragma mark - 监听加载进度
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    HWWeakSelf(weakSelf)
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        if (object == _webView) {
            [weakSelf.progressView setAlpha:1.0f];
            [weakSelf.progressView setProgress:weakSelf.webView.estimatedProgress animated:YES];
            if(weakSelf.webView.estimatedProgress >= 1.0f) {
                [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{
                    [weakSelf.progressView setAlpha:0.0f];
                } completion:^(BOOL finished) {
                    [weakSelf.progressView setProgress:0.0f animated:NO];
                }];
            }
        } else {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    }  else if ([keyPath isEqualToString:@"title"]) {
        HWLog(@"%@",weakSelf.webView.title);
        if (object == weakSelf.webView) {
            weakSelf.title = weakSelf.webView.title;
        } else {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
// 当对象即将销毁的时候调用
- (void)dealloc {
    HWLog(@"webView释放");
    HWWeakSelf(weakSelf)
    [_webView removeObserver:weakSelf forKeyPath:@"estimatedProgress"];
    [_webView removeObserver:weakSelf forKeyPath:@"title"];
    _webView.navigationDelegate = nil;
}

你可能感兴趣的:(WKWebView基本使用)