#import "QLCWebViewController.h"
#import <WebKit/WebKit.h>
@interface QLCWebViewController ()
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet WKWebView *webView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *backItem;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *forwardItem;
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
@end
@implementation QLCWebViewController
- (IBAction)goBack:(id)sender {
[self.webView goBack];
}
- (IBAction)goForward:(id)sender {
[self.webView goForward];
}
- (IBAction)reload:(id)sender {
[self.webView reload];
}
#pragma mark - 生命周期方法
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
_webView.frame = self.contentView.bounds;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// 添加webView
WKWebView *webView = [[WKWebView alloc] init];
_webView = webView;
[self.contentView addSubview:webView];
// 展示网页
NSURLRequest *request = [NSURLRequest requestWithURL:_url];
[webView loadRequest:request];
// KVO监听属性改变
/*
Observer:观察者
KeyPath:观察webView哪个属性
options:NSKeyValueObservingOptionNew:观察新值改变
KVO注意点.一定要记得移除
*/
[webView addObserver:self forKeyPath:@"canGoBack" options:NSKeyValueObservingOptionNew context:nil];
[webView addObserver:self forKeyPath:@"canGoForward" options:NSKeyValueObservingOptionNew context:nil];
[webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
// 进度条
[webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
}
// 只要观察对象属性有新值就会调用
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
self.backItem.enabled = self.webView.canGoBack;
self.forwardItem.enabled = self.webView.canGoForward;
self.title = self.webView.title;
self.progressView.progress = self.webView.estimatedProgress;
self.progressView.hidden = self.webView.estimatedProgress >= 1;
}
#pragma mark - 对象被销毁
- (void)dealloc
{
[self.webView removeObserver:self forKeyPath:@"canGoBack"];
[self.webView removeObserver:self forKeyPath:@"title"];
[self.webView removeObserver:self forKeyPath:@"canGoForward"];
[self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
}
@end