#import "ZDBaseViewController.h"
NS_ASSUME_NONNULL_BEGIN
@interface ZDWebViewViewController : ZDBaseViewController
@end
NS_ASSUME_NONNULL_END
#import "ZDWebViewViewController.h"
#import <WebKit/WebKit.h>
@interface ZDWebViewViewController ()<WKUIDelegate,WKNavigationDelegate>
@property(nonatomic,strong)WKWebView *wkWebView;
@end
@implementation ZDWebViewViewController
- (void)viewDidLoad {
[super viewDidLoad];
//将网页视图添加到当前视图上
[self.view addSubview:self.wkWebView];
//masonry适配当前视图
[self.wkWebView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.view);
}];
[self loadRequest];
}
//加载网址
- (void)loadRequest{
NSString *urlStr = [NSString stringWithFormat:@"https://www.baidu.com"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:urlStr]];
[self.wkWebView loadRequest:request];
}
//初始化网页视图
- (WKWebView *)wkWebView{
if (!_wkWebView) {
_wkWebView = [[WKWebView alloc] init];
_wkWebView.navigationDelegate = self;
}
return _wkWebView;
}
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
}
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
// [self getCookie];
}
// 接收到服务器跳转请求之后调用
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
}
// 在收到响应后,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
NSLog(@"%@",navigationResponse.response.URL.absoluteString);
//允许跳转
decisionHandler(WKNavigationResponsePolicyAllow);
//不允许跳转
//decisionHandler(WKNavigationResponsePolicyCancel);
}
// 在发送请求之前,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSLog(@"%@",navigationAction.request.URL.absoluteString);
//允许跳转//打印出
decisionHandler(WKNavigationActionPolicyAllow);
//不允许跳转
//decisionHandler(WKNavigationActionPolicyCancel);
}
@end